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:
- 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:
- Singlecast Delegate
- 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.
- public delegate void Mydel();
-
- class Program
- {
- public static void Display()
- {
- Console.WriteLine("I am in the Display Method");
- }
-
- static void Main(string[] args)
- {
- Mydel Objmydel = new Mydel(Display);
-
- Objmydel.Invoke();
-
- Console.ReadLine();
- }
- }
The output is:
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.
- class Program
- {
- public delegate void Calculate(int a, int b);
-
- static void Main(string[] args)
- {
- int a = 5;
- int b = 50;
- Calculate addNumber = new Calculate(AddNumber);
- Calculate multiplyNumber = new Calculate(MultiplyNumber);
- Calculate multiCast = (Calculate)Delegate.Combine(addNumber, multiplyNumber);
- multiCast.Invoke(a, b);
- Console.ReadLine();
- }
- public static void AddNumber(int a, int b)
- {
- Console.WriteLine("Adding Number");
- Console.WriteLine(a + b);
- }
- public static void MultiplyNumber(int a, int b)
- {
- Console.WriteLine("Multiply Number");
- Console.WriteLine(a * b);
- }
- }
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.
We can do this using another way also. Let us check now.
- class Program
- {
- public delegate void Calculate(int a, int b);
-
- static void Main(string[] args)
- {
- int a = 5;
- int b = 50;
- Calculate obj = new Calculate(AddNumber);
- obj += MultiplyNumber;
- obj.Invoke(a, b);
- Console.ReadLine();
- }
- public static void AddNumber(int a, int b)
- {
- Console.WriteLine("Adding Number");
- Console.WriteLine(a + b);
- }
- public static void MultiplyNumber(int a, int b)
- {
- Console.WriteLine("Multiply Number");
- Console.WriteLine(a * b);
- }
- }
Here we have instantiated a delegate just once and referenced both of the methods and invoked it. The result is, as expected, the same.