Delegates in C# Language

Delegates in C# are similar to function pointers in C or C++. It is a reference type that holds a method reference. The System.delegate namespace is imported in the application to use delegates.

The reference of a delegate can be changed at runtime. A delegate can refer to a method that has the same signature as that of the delegate.

The syntax for declaring a delegate is:

delegate <return type> <delegate-name> <parameter list>

For example:

  1. public delegate void Calculate(int a, int b); 

This is a delegate having the name “Calculate” that has two parameters. It is of type void and uses the delegate keyword.

There are two types of delegates:

  1. Singlecast Delegate
  2. Multicast Delegate

Single Cast Delegate: This delegate holds a reference of one method. Let us see this using a demo application. Open Visual Studio. Create a new project. Select a console application and rovide the following code.

  1.   public delegate void Mydel();  
  2.   
  3.   class Program  
  4.   {  
  5.       public static void Display()  
  6.       {  
  7.           Console.WriteLine("I am in the Display Method");  
  8.       }  
  9.   
  10.       static void Main(string[] args)  
  11.       {  
  12.           Mydel Objmydel = new Mydel(Display);  
  13.   
  14.           Objmydel.Invoke();  
  15.   
  16.           Console.ReadLine();  
  17.       }  
  18.   } 

The output is:

The output

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 suggests a Multicast delegate contains references for more than one method. Let us have a look at the code now.

  1.  class Program  
  2.  {  
  3.      public delegate void Calculate(int a, int b);  
  4.   
  5.      static void Main(string[] args)  
  6.      {  
  7.          int a = 5;  
  8.          int b = 50;  
  9.          Calculate addNumber = new Calculate(AddNumber);  
  10.          Calculate multiplyNumber = new Calculate(MultiplyNumber);  
  11.          Calculate multiCast = (Calculate)Delegate.Combine(addNumber, multiplyNumber);  
  12.          multiCast.Invoke(a, b);  
  13.          Console.ReadLine();  
  14.      }  
  15.      public static void AddNumber(int a, int b)  
  16.      {  
  17.          Console.WriteLine("Adding Number");  
  18.          Console.WriteLine(a + b);  
  19.      }  
  20.      public static void MultiplyNumber(int a, int b)  
  21.      {  
  22.          Console.WriteLine("Multiply Number");  
  23.          Console.WriteLine(a * b);  
  24.      }  
  25.  } 

Here two static methods, AddNumber and MultiplyNumber, are present. A delegate named “Calculate” is declared that has two arguments. It is instantiated and both function names are passed to it. A third instance is created that combines both of the instances of the delegate and executes both of these methods.

When we run the application we get the following output.

output

We can do this using another way also. Let us check now.

  1. class Program  
  2. {  
  3.     public delegate void Calculate(int a, int b);  
  4.   
  5.     static void Main(string[] args)  
  6.     {  
  7.         int a = 5;  
  8.         int b = 50;  
  9.         Calculate obj = new Calculate(AddNumber);  
  10.         obj += MultiplyNumber;  
  11.         obj.Invoke(a, b);  
  12.         Console.ReadLine();  
  13.     }  
  14.     public static void AddNumber(int a, int b)  
  15.     {  
  16.         Console.WriteLine("Adding Number");  
  17.         Console.WriteLine(a + b);  
  18.     }  
  19.     public static void MultiplyNumber(int a, int b)  
  20.     {  
  21.         Console.WriteLine("Multiply Number");  
  22.         Console.WriteLine(a * b);  
  23.     }  

Here we have instantiated a delegate just once and referenced both of the methods and invoked it. The result is, as expected, the same.

result


Similar Articles