Introduction To Delegate

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 
  1. Call method by creating delegate object.
  2. It improves the performance of application.
  3. Method calls asynchronously.so, make your application more faster.
Declaration  
  1. public delegate type_of_delegate delegate_name() 
Example 
  1. public delegate intmydelegate(int value1,int value2)   
Note
  1. You can use delegates with parameters and without parameters list.
  2. 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 
  1. public delegate int Delegate_add(int value1,int Value2);  
  2. class Add  
  3. {  
  4.       static int Add(int passvalue1,int passvalue2)  
  5.       {  
  6.             return  passvalue1+passvalue2;  
  7.       }   
  8.       static void Main(string[] args)  
  9.       {  
  10.          //create delegate object   
  11.          Delegate_add obj=new Delegate_add(Add);  
  12.          Console.Write("Enter values");  
  13.          int a=Int32.Parse(Console.ReadLine());  
  14.          int b=Int32.Parse(Console.ReadLine());  
  15.          //use delegate here  
  16.          int result= obj(a,b);  
  17.          Console.WriteLine("Result:"+result);   
  18.          Console.ReadLine();   
  19.       }   
  20. }   
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,

  1. 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,

  1. int result= obj(a,b);   

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

  1. delegate void Delegate_Multicast(int x, int y);  
  2.   
  3. Class Class2  
  4. {  
  5.   
  6.    static void Method1(int x, int y)  
  7.   
  8.    {  
  9.   
  10.       Console.WriteLine("You r in Method 1");  
  11.   
  12.    }  
  13.   
  14.    static void Method2(int x, int y)  
  15.   
  16.    {  
  17.   
  18.       Console.WriteLine("You r in Method 2");  
  19.   
  20.    }  
  21.   
  22.    public static void Main(string[] args)  
  23.   
  24.    {  
  25.   
  26.       Delegate_Multicast func = new Delegate_Multicast(Method1);  
  27.   
  28.       func += new Delegate_Multicast(Method2);  
  29.   
  30.       func(1,2); // Method1 and Method2 are called  
  31.   
  32.       func -= new Delegate_Multicast(Method1);  
  33.   
  34.       func(2,3); // Only Method2 is called  
  35.   
  36.    }  
  37.   
  38. }  

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,

  1. Delegate_Multicast func = new Delegate_Multicast(Method1);   

Then the Delegate is added using the += operator and removed using the -= operator.