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
-
- public delegate void Math_Operation(int a, int b);
-
-
- public void Add(int a, int b)
- {
- Console.WriteLine("Addition = " + (a + b));
- }
Delegates can be used as in the following.
- Single-cast delegate
When a delegate variable holds a single method then it’s called a Single cast delegate.
Let’s see an example.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Diagnostics;
- using System.Runtime.InteropServices;
-
- namespace ConsoleApplication1
- {
- class Class1
- {
-
- public delegate void Math_Operation(int a, int b);
-
-
- public void Add(int a, int b)
- {
- Console.WriteLine("Addition = " + (a + b));
- }
- static void Main(string[] args)
- {
-
- Class1 obj = new Class1();
-
- Math_Operation op = new Math_Operation(obj.Add);
-
- op(10, 5);
- Console.ReadKey();
- }
- }
-
- }
Output
- Multicast delegate
When a delegate variable holds multiple methods then it’s called a single cast delegate (this is also called delegate chaining).
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Diagnostics;
- using System.Runtime.InteropServices;
-
-
- namespace ConsoleApplication1
- {
- class Class1
- {
-
- public delegate void Math_Operation(int a, int b);
-
-
- public void Add(int a, int b)
- {
- Console.WriteLine("Addition = " + (a + b));
- }
-
- public void Sub(int a, int b)
- {
- int result = (a - b);
- Console.WriteLine("Substraction = " + (a - b));
- }
-
- static public void Mult(int a, int b)
- {
- Console.WriteLine("Multiplication = " + (a * b));
- }
-
- public void Div(int a, int b)
- {
- Console.WriteLine("Division = " + (a / b));
- }
-
- static void Main(string[] args)
- {
-
- Class1 obj = new Class1();
-
- Math_Operation op = new Math_Operation(obj.Add);
- Math_Operation op1 = new Math_Operation(obj.Sub);
-
- op += op1;
-
- op += Mult;
- op += obj.Div;
- op(10, 5);
-
- Console.WriteLine("\nRemoving Mult or Div method from delegate");
- op -= Mult;
- op -= obj.Div;
- op(20, 10);
- Console.ReadKey();
- }
- }
-
- }
Output
Notes
- All the methods we assign to a delegate have the same signature as the delegate declaration.
- A delegate instance may encapsulate either a static or an instance (non-static) method.
- If we try to assign a method to a delegate variable with a different signature then we get an error like
Thanks for reading.