What is a C# delegate?
The C# delegate is a reference data type that defines the method and their signature. The delegate stores the address of one or more methods. When a delegate for a method assigned, it behaves exactly like the method. It can be invoked like any other method with parameters and return value.
A delegate simply stores the reference of the methods with the same signatures such as return type and parameter list. A delegate can be invoked as like a class by creating an object for the delegate. The delegate object allows running the methods at run time without invoking it directly.
This post was originally posted on DevelopersDoors
Usage of delegate:
When an event needs to be used then delegates can be used. Multicast delegates can be used for invoking many events at a time. When a class need to have more than one implementation of the method and to encapsulate a static method.
Refer for when to use delegates and interfaces -> https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/ms173173(v=vs.100)
Declaring a delegate:
The System.Delegate
class is the base class for the delegate declaration. The delegate keyword is used to declare a delegate. The return type and parameters should same as the methods.
Eg:
public delegate int CalDelegate(int num1,int num2);
Instantiation and invoking delegates:
To instantiate a delegate, an instance is created similar to how a class instance is created. When a delegate is instantiated, the address of the method is stored in the instance.
Eg:Calculator cal=new Calculator();
CalDelegate sumDel=new CalDelegate(cal.sum);
CalDelegate mulDel=new CalDelegate(cal.multiply);
CalDelegate
is the delegate created and the sumDel
is the delegate instance instantiated to CalDelegate
with the reference to the sum()
method in cal instance and mulDel
is for reference of multiply()
method.
To invoke the delegate, calling the delegate instance with the parameter list referenced by the delegate with the same data type.
Eg:sumDel(10, 12);
mulDel(10,12);
using System;
public delegate int CalDelegate(int num1,int num2);
public class Calculator
{
public int sum(int num1,int num2)
{
return num1+num2;
}
public int multiply(int num1,int num2)
{
return num1*num2;
}
}
public class Program
{
public static void Main()
{
Calculator cal=new Calculator();
CalDelegate sumDel=new CalDelegate(cal.sum); // sumDel for sum() reference
CalDelegate mulDel=new CalDelegate(cal.multiply); //mulDel for multiply() reference
Console.WriteLine(sumDel(10,12));
Console.WriteLine(mulDel(10,12));
}
}
Output:
22
120
Multicast delegates:
To have a reference of more than one method in delegate multicast delegates is used. This mainly used in the multi-event invocation. The += operator is used to add the methods referred to delegate and -= operator is used to remove the methods. The methods are called in First-In-First-Out(FIFO) order.
Remember when using multicast delegates use void as return types because the use of multiple methods will clash in return type. So void is safe to avoid runtime exception.
Eg:
CalDelegate simpleDel=new CalDelegate(cal.sum);
simpleDel += new CalDelegate(cal.multiply);
simpleDel -= new CalDelegate(cal.sum);
using System;
public delegate void CalDelegate(int num1,int num2);
public class Calculator
{
public void sum(int num1,int num2)
{
Console.WriteLine(num1+num2);
}
public void multiply(int num1,int num2)
{
Console.WriteLine(num1*num2);
}
}
public class Program
{
public static void Main()
{
Calculator cal=new Calculator();
CalDelegate simpleDel=new CalDelegate(cal.sum);
simpleDel+=new CalDelegate(cal.multiply); //multicasting using += operator
simpleDel(10,12);
}
}
Output:
22
120
The above delegate example is used with multicast delegates in which the firstsum()
method is referenced to thesimpleDel
delegate. Thenmultiply()
method is added to multicast using +=
operator. To remove the methods from delegate simply use -=
operator. As said multicast delegate run in FIFO, firstsum()
delegate reference is run and thenmultiply()
with output 22 and 120 respectively.
For documentation of delegates refer -> https://docs.microsoft.com/en-in/dotnet/api/system.delegate?view=netcore-3.1
In next post, I will explain synchronous delegate, asynchronous delegate and covariance and contravariance delegates.
For my other C# archives -> Go.
If you enjoy my post please share it and if any suggestion please comment on it.
Top comments (0)