We can say delegates are a .NET object which points to a method that matches its specific signature or delegates are function pointers that point to function of matching signatures.
Function pointers which are extensively used in c/c++ to points to a function holds only the memory address of the function, it doesn’t carry further information about the function parameters, return type etc.
In other words delegates are function pointers that point to function of matching signatures. Function pointers which are extensively used in c/c++ to points to a function holds only the memory address of the function, it doesn’t carry further information about the function parameters, return type etc.
In .NET framework has introduced a type-safe mechanism called delegates, with automatic verification of the signature by the compiler. So the delegates are type-safe, object oriented, secure .NET objects which can be used to invoke methods of matching signature.
While using delegates it is very necessary to make sure that the functions which the delegates points has the same number of argument type and same return type.
For example
if we have a method that takes a single string as a parameter and another method that takes two string parameters, then we need to have two separate delegate type for each method.
Types of Delegate
Delegates are of two types. They are
- Single cast delegate
- Multicast Delegates
Single cast delegate
A delegate is called single cast delegate if it invokes a single method. Its derive from the System.Delegate class. See the below sample :
using System;
namespace Delegates
{
public delegate int DelegateToMethod(int x, int y);
public class Math
{
public static int Add(int first, int second)
{
return first + second;
}
public static int Multiply(int first, int second)
{
return first * second;
}
}
public class DelegateApp
{
public static void Main()
{
DelegateToMethod aDelegate = new DelegateToMethod(Math.Add);
DelegateToMethod mDelegate = new DelegateToMethod(Math.Multiply);
Console.WriteLine("Calling the method Math.Add() through the aDelegate object");
Console.WriteLine(aDelegate(5,5));
Console.WriteLine("Calling the method Math.Multiply() through the mDelegate object");
Console.WriteLine(mDelegate(5,5));
Console.ReadLine();
}
}
}
The output for above sample is
10
Calling the method Math.Multiply() through the mDelegate object
25
Multicast delegate
A multicast delegate is an object that maintains a linked list of delegates. Invoking the delegate invokes each delegate in the same order that it has been added to the linked list.
For an example
we have created an object (a delegate) called multiDelegate of type Math.DelegateToMethod and assigned a null value to this object. The next, assigns a new delegate object that encapsulates the method math.Add(). Using the operator += we assign more delegate objects (thus creating a multicast delegate) to the delegate multiDelegate.
It invoking the delegate invokes all the delegates maintained in its linked list, which in turn calls the encapsulated methods.
See the below sample :
using System;
namespace Delegates
{
public class Math
{
// note that the delegate now is a nested type of the Math class
public delegate void DelegateToMethod(int x, int y);
public void Add(int first, int second)
{
Console.WriteLine("The method Add() returns {0}", first + second);
}
public void Multiply(int first, int second)
{
Console.WriteLine("The method Multiply() returns {0}", first * second);
}
public void Divide(int first, int second)
{
Console.WriteLine("The method Divide() returns {0}", first / second);
}
}
public class DelegateApp
{
public static void Main()
{
Math math = new Math();
Math.DelegateToMethod multiDelegate = null;
multiDelegate = new Math.DelegateToMethod(math.Add);
multiDelegate += new Math.DelegateToMethod(math.Multiply);
multiDelegate += new Math.DelegateToMethod(math.Divide);
multiDelegate(5,5);
Console.ReadLine();
}
}
}
The output of the above sample is
The method Add() returns 10
The method Multiply() returns 25
The method Divide() returns 1
The statement multiDelegate(5,5); is similar to our previous example in which we have created three delegate objects and then calling each of them separately.
You can remove a delegate object from the linked list using the operator -=.
Where are Delegates used?
The most common example of using delegates is in events. You define a method that contains code for performing various tasks when an event (such as a mouse click) takes place. This method needs to be invoked by the runtime when the event occurs. Hence this method, that you defined, is passed as a parameter to a delegate.
Happy coding!!!