Introduction
Delegate is good feature in C#.NET. It helps to make code very simple,reliable and fast.In this article will see the single delegate and multicast delegate and their usage.
What is a Delegate?
Delegate is use to register method i.e. holds reference to the method or pointing to that method.
Advantages
- Call method by creating delegate object.
- It improves the performance of application.
- Method calls asynchronously.so, make your application more faster.
Declaration
- public delegate type_of_delegate delegate_name()
Example
- public delegate intmydelegate(int value1,int value2)
Note
- You can use delegates with parameters and without parameters list.
- You should follow the same syntax as in the method.(If you are referring to the method with two int parameters and int return type, the delegates which you are declaring should be in the same format. This is why it is referred to as type safe function pointer.)
Sample program using delegate
- public delegate int Delegate_add(int value1,int Value2);
- class Add
- {
- static int Add(int passvalue1,int passvalue2)
- {
- return passvalue1+passvalue2;
- }
- static void Main(string[] args)
- {
-
- Delegate_add obj=new Delegate_add(Add);
- Console.Write("Enter values");
- int a=Int32.Parse(Console.ReadLine());
- int b=Int32.Parse(Console.ReadLine());
-
- int result= obj(a,b);
- Console.WriteLine("Result:"+result);
- Console.ReadLine();
- }
- }
Explanation
Here I have used a small program which demonstrates the use of delegate.
The delegate "Delegate_add" is declared with int return type and accepts only two integer parameters.
Inside the class, the method named Add is defined with int return type and two integer parameters. (The delegate and method have the same signature and parameter type.)
Inside the Main method, the delegate instance is created and the function name is passed to the delegate instance as follows,
- Delegate_add obj=new Delegate_add(Add);
After this, we are accepting the two values from the user and passing those values to the delegate as we do using method,
Here delegate object encapsulates the method functionalities and returns the result as we specified in the method.
What is Multicast Delegate?
It is a delegate which holds the reference of more than one method.
Multicast delegates must contain only methods that return void, else there is a run-time exception.
Simple Program using Multicast Delegate
- delegate void Delegate_Multicast(int x, int y);
-
- Class Class2
- {
-
- static void Method1(int x, int y)
-
- {
-
- Console.WriteLine("You r in Method 1");
-
- }
-
- static void Method2(int x, int y)
-
- {
-
- Console.WriteLine("You r in Method 2");
-
- }
-
- public static void Main(string[] args)
-
- {
-
- Delegate_Multicast func = new Delegate_Multicast(Method1);
-
- func += new Delegate_Multicast(Method2);
-
- func(1,2);
-
- func -= new Delegate_Multicast(Method1);
-
- func(2,3);
-
- }
-
- }
Explanation
In the above example, you can see that two methods are defined, named method1 and method2, which take two integer parameters and return type as void.
In the main method, the Delegate object is created using the following statement,
- Delegate_Multicast func = new Delegate_Multicast(Method1);
Then the Delegate is added using the += operator and removed using the -= operator.