Delegates in C#

Basically a delegate is similar to a C function pointer with a specific signature (return type, parameter type and order and so on) where the function can be assigned like a variable and called at runtime on dynamic condition. When we instantiate a delegate, we can associate its instance with a method with a compatible signature and return type. Delegates provide maximum flexibility to implement the functionality you want at runtime. A delegate can be seen as a placeholder for a method or multiple methods.

Delegates are used to pass methods as an argument to other methods. To invoke a delegate, one or more methods are required with the same signature. A delegate object is first created (similar to a class object created). The delegate object will basically hold a reference of a function. The function will then can be called via the delegate object.

The following is the syntax for declaring a delegate:

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

Example

  1. //Declare a Delegate  
  2. public delegate void Math_Operation(int a, int b);  
  3.   
  4. //Declare a Method with the same signature as delegate  
  5. public void Add(int a, int b)  
  6. {  
  7.    Console.WriteLine("Addition = " + (a + b));  
  8. }  
Delegates can be used as in the following.
  1. Single-cast delegate

    When a delegate variable holds a single method then it’s called a Single cast delegate.

    Let’s see an example.
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Text;  
    5. using System.Diagnostics;  
    6. using System.Runtime.InteropServices;  
    7.   
    8. namespace ConsoleApplication1  
    9. {  
    10.     class Class1  
    11.     {  
    12.         //Declare a Delegate  
    13.         public delegate void Math_Operation(int a, int b);  
    14.           
    15.         //Declare a Method with the same signature as delegate  
    16.         public void Add(int a, int b)  
    17.         {  
    18.             Console.WriteLine("Addition = " + (a + b));  
    19.         }  
    20.         static void Main(string[] args)  
    21.         {  
    22.             //Declare instance of class  
    23.             Class1 obj = new Class1();  
    24.             //Declare instance of delegate  
    25.             Math_Operation op = new Math_Operation(obj.Add);  
    26.             //Invoke the delegate object  
    27.             op(10, 5);  
    28.             Console.ReadKey();  
    29.         }  
    30.     }  
    31.   
    32. }  
    Output

    run

  2. Multicast delegate

    When a delegate variable holds multiple methods then it’s called a single cast delegate (this is also called delegate chaining).
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Text;  
    5. using System.Diagnostics;  
    6. using System.Runtime.InteropServices;  
    7.   
    8.   
    9. namespace ConsoleApplication1  
    10. {  
    11.     class Class1  
    12.     {  
    13.         // Declare a Delegate  
    14.         public delegate void Math_Operation(int a, int b);  
    15.   
    16.         // Declare multiple Method with the same signature as delegate  
    17.         public void Add(int a, int b)  
    18.         {  
    19.             Console.WriteLine("Addition = " + (a + b));  
    20.         }  
    21.   
    22.         public void Sub(int a, int b)  
    23.         {  
    24.             int result = (a - b);  
    25.             Console.WriteLine("Substraction = " + (a - b));  
    26.         }  
    27.   
    28.         static public void Mult(int a, int b)  
    29.         {  
    30.             Console.WriteLine("Multiplication = " + (a * b));  
    31.         }  
    32.   
    33.         public void Div(int a, int b)  
    34.         {  
    35.             Console.WriteLine("Division = " + (a / b));  
    36.         }  
    37.   
    38.         static void Main(string[] args)  
    39.         {  
    40.             // Declare an instance of class  
    41.             Class1 obj = new Class1();  
    42.             // Declare two instance of delegate  
    43.             Math_Operation op = new Math_Operation(obj.Add);  
    44.             Math_Operation op1 = new Math_Operation(obj.Sub);  
    45.             // Delegate op and op1 are combine  
    46.             op += op1;  
    47.             // Adding another method to delegate  
    48.             op += Mult;  
    49.             op += obj.Div;  
    50.             op(10, 5);  
    51.             // Removing Mult or Div method from delegate op  
    52.             Console.WriteLine("\nRemoving Mult or Div method from delegate");  
    53.             op -= Mult;  
    54.             op -= obj.Div;  
    55.             op(20, 10);  
    56.             Console.ReadKey();  
    57.         }  
    58.     }  
    59.   
    60. }  
    Output

    Output

Notes

  1. All the methods we assign to a delegate have the same signature as the delegate declaration.
  2. A delegate instance may encapsulate either a static or an instance (non-static) method.
  3. If we try to assign a method to a delegate variable with a different signature then we get an error like

Thanks for reading.


Similar Articles