Professional Documents
Culture Documents
C# Raw
C# Raw
• Step 2: Drag the ToolTip from the ToolBox and drop it on the form. When you drag
and drop this ToolTip on the form it will automatically add to the properties(named
as ToolTip on ToolTip1) of every controls present in the current windows from.
• Step 3: After drag and drop you will go to the properties of the ToolTip control to
modify ToolTip according to your requirement.
Output:
2. Run-Time: It is a little bit trickier than the above method. In this method, you can create a
ToolTip control programmatically with the help of syntax provided by the ToolTip class. The
following steps show how to set the create ToolTip dynamically:
• Step 1: Create a ToolTip control using the ToolTip() constructor is provided by the
ToolTip class.
• // Creating a ToolTip control
• ToolTip t_Tip = new ToolTip();
• Step 2: After creating ToolTip control, set the property of the ToolTip control
provided by the ToolTip class.
• // Seting the properties of ToolTip
• t_Tip.Active = true;
• t_Tip.AutoPopDelay = 4000;
• t_Tip.InitialDelay = 600;
• t_Tip.IsBalloon = true;
• t_Tip.ToolTipIcon = ToolTipIcon.Info;
• t_Tip.SetToolTip(box1, "Name should start with Capital letter");
• t_Tip.SetToolTip(box2, "Password should be greater than 8 words");
Example:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp34 {
public Form1()
{
InitializeComponent();
}
Output:
C# - Polymorphism
The word polymorphism means having many forms. In object-oriented programming paradigm,
polymorphism is often expressed as 'one interface, multiple functions'.
Polymorphism can be static or dynamic. In static polymorphism, the response to a function is
determined at the compile time. In dynamic polymorphism, it is decided at run-time.
Static Polymorphism
The mechanism of linking a function with an object during compile time is called early binding. It is
also called static binding. C# provides two techniques to implement static polymorphism. They are
−
• Function overloading
• Operator overloading
We discuss operator overloading in next chapter.
Function Overloading
You can have multiple definitions for the same function name in the same scope. The definition of
the function must differ from each other by the types and/or the number of arguments in the
argument list. You cannot overload function declarations that differ only by return type.
The following example shows using function print() to print different data types −
Live Demo
using System;
namespace PolymorphismApplication {
class Printdata {
void print(int i) {
Console.WriteLine("Printing int: {0}", i );
}
void print(double f) {
Console.WriteLine("Printing float: {0}" , f);
}
void print(string s) {
Console.WriteLine("Printing string: {0}", s);
}
static void Main(string[] args) {
Printdata p = new Printdata();
1. using System;
2. public class Shape{
3. public virtual void draw(){
4. Console.WriteLine("drawing...");
5. }
6. }
7. public class Rectangle: Shape
8. {
9. public override void draw()
10. {
11. Console.WriteLine("drawing rectangle...");
12. }
13.
14. }
15. public class Circle : Shape
16. {
17. public override void draw()
18. {
19. Console.WriteLine("drawing circle...");
20. }
21.
22. }
23. public class TestPolymorphism
24. {
25. public static void Main()
26. {
27. Shape s;
28. s = new Shape();
29. s.draw();
30. s = new Rectangle();
31. s.draw();
32. s = new Circle();
33. s.draw();
34.
35. }
36. }
Output:
drawing...
drawing rectangle...
drawing circle...
When you have a function defined in a class that you want to be implemented in an inherited
class(es), you use virtual functions. The virtual functions could be implemented differently in
different inherited class and the call to these functions will be decided at runtime.
Dynamic polymorphism is implemented by abstract classes and virtual functions.
C# Multithreading
• Difficulty Level : Medium
• Last Updated : 22 Apr, 2020
Multitasking is the simultaneous execution of multiple tasks or processes over a certain time
interval. Windows operating system is an example of multitasking because it is capable of
running more than one process at a time like running Google Chrome, Notepad, VLC player,
etc. at the same time. The operating system uses a term known as a process to execute all these
applications at the same time. A process is a part of an operating system that is responsible for
executing an application. Every program that executes on your system is a process and to run
the code inside the application a process uses a term known as a thread.
A thread is a lightweight process, or in other words, a thread is a unit which executes the code
under the program. So every program has logic and a thread is responsible for executing this
logic. Every program by default carries one thread to executes the logic of the program and
the thread is known as the Main Thread, so every program or application is by default single-
threaded model. This single-threaded model has a drawback. The single thread runs all the
process present in the program in synchronizing manner, means one after another. So, the
second process waits until the first process completes its execution, it consumes more time in
processing.
For example, we have a class named as Geek and this class contains two different methods,
i.e method1, method2. Now the main thread is responsible for executing all these methods, so
the main thread executes all these methods one by one.
Example:
// C# program to illustrate the
// concept of single threaded model
using System;
using System.Threading;
// Driver Class
public class GFG {
// Main Method
static public void Main()
{
Output:
Method1 is : 0
Method1 is : 1
Method1 is : 2
Method1 is : 3
Method1 is : 4
Method1 is : 5
Method1 is : 6
Method1 is : 7
Method1 is : 8
Method1 is : 9
Method1 is : 10
Method2 is : 0
Method2 is : 1
Method2 is : 2
Method2 is : 3
Method2 is : 4
Method2 is : 5
Method2 is : 6
Method2 is : 7
Method2 is : 8
Method2 is : 9
Method2 is : 10
Advantages of Multithreading:
• It executes multiple process simultaneously.
• Maximize the utilization of CPU resources.
• Time sharing between multiple process.
C# | Encapsulation
• Difficulty Level : Easy
• Last Updated : 23 Jan, 2019
Encapsulation is defined as the wrapping up of data under a single unit. It is the mechanism
that binds together code and the data it manipulates. In a different way, encapsulation is a
protective shield that prevents the data from being accessed by the code outside this shield.
• Technically in encapsulation, the variables or data of a class are hidden from any
other class and can be accessed only through any member function of own class in
which they are declared.
• As in encapsulation, the data in a class is hidden from other classes, so it is also
known as data-hiding.
• Encapsulation can be achieved by: Declaring all the variables in the class as
private and using C# Properties in the class to set and get the values of variables.
Example:
// C# program to illustrate encapsulation
using System;
get
{
return studentName;
}
set
{
studentName = value;
}
get
{
return studentAge;
}
set
{
studentAge = value;
}
// Driver Class
class GFG {
// Main Method
static public void Main()
{
// creating object
DemoEncap obj = new DemoEncap();
Output:
Name: Kavitha
Age: 21
Explanation: In the above program the class DemoEncap is encapsulated as the variables are
declared as private. To access these private variables we are using the Name and Age
accessors which contains the get and set method to retrieve and set the values of private
fields. Accessors are defined as public so that they can access in other class.
Advantages of Encapsulation:
• Data Hiding: The user will have no idea about the inner implementation of the class.
It will not be visible to the user that how the class is stored values in the variables.
He only knows that we are passing the values to accessors and variables are getting
initialized to that value.
• Increased Flexibility: We can make the variables of the class as read-only or write-
only depending on our requirement. If we wish to make the variables as read-only
then we have to only use Get Accessor in the code. If we wish to make the variables
as write-only then we have to only use Set Accessor.
• Reusability: Encapsulation also improves the re-usability and easy to change with
new requirements.
• Testing code is easy: Encapsulated code is easy to test for unit testing
Code for creating menu bar
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}