Delegates
Delegates in C# are similar to function pointers in C or C++. It is a reference type that holds a method reference.System.delegate namespace is imported in the application to use delegates.
The reference of delegate can be changed at runtime. A delegate can refer to a method, which have the same signature as that of the delegate.
The syntax for declaring delegate is
1
2
3
| delegate < return type> < delegate -name> <parameter list> For ex: public delegate void Calculate( int a, int b); |
This is a delegate having the name “Calculate” which has two parameters. It is of type void and uses the delegate keyword.
There are two types of delegates:
- Singlecast Delegate
- Multicast Delegate
Single Cast Delegate: This delegate holds reference of one method. Let us see this with the help of a demo application. Open Visual Studio. Create New Project. Select a console application and write down the below code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| public delegate void Mydel(); class Program { public static void Display() { Console.WriteLine( "I am in the Display Method" ); } static void Main( string [] args) { Mydel Objmydel = new Mydel(Display); Objmydel.Invoke(); Console.ReadLine(); } } |
The output is
So instead of calling the method directly we instantiate the delegate and pass the reference of the method in the delegate object. Once we invoke the delegate it automatically calls the Display method and the content of the Display method is displayed on the output screen.
Multicast Delegate:
Now let us check a demo for Multicast Delegate. As the name suggest Multicast delegate contains reference of more than one method. Let us have a look at the code now.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
| class Program { public delegate void Calculate( int a, int b); static void Main( string [] args) { int a = 5; int b = 50; Calculate addNumber = new Calculate(AddNumber); Calculate multiplyNumber = new Calculate(MultiplyNumber); Calculate multiCast = (Calculate)Delegate.Combine(addNumber, multiplyNumber); multiCast.Invoke(a, b); Console.ReadLine(); } public static void AddNumber( int a, int b) { Console.WriteLine( "Adding Number" ); Console.WriteLine(a + b); } public static void MultiplyNumber( int a, int b) { Console.WriteLine( "Multiply Number" ); Console.WriteLine(a * b); } } |
Here two static methods AddNumber and MultiplyNumber are present. A delegate named “Calculate” is declared which has two arguments. It is instantiated and both function names are passed to it. A third instance is created which combines both the instances of delegate and executes both of these methods.
When we run the application we get the following output.
We can do this using another way also. Let us check now.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
| class Program { public delegate void Calculate( int a, int b); static void Main( string [] args) { int a = 5; int b = 50; Calculate obj = new Calculate(AddNumber); obj += MultiplyNumber; obj.Invoke(a, b); Console.ReadLine(); } public static void AddNumber( int a, int b) { Console.WriteLine( "Adding Number" ); Console.WriteLine(a + b); } public static void MultiplyNumber( int a, int b) { Console.WriteLine( "Multiply Number" ); Console.WriteLine(a * b); } } |
Here we have instantiated delegate just once and referenced both the methods and invoked it. The result is as expected the same.
No comments:
Post a Comment