When we talk about delegates in .NET then one thing that comes to our mind is what delegate means to a novice developer. In simple words we can say delegates are a .NET object which points to a method that matches its specific signature.
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. On the other hand .NET framework has introduced a type-safe mechanism called delegates, with automatic verification of the signature by the compiler.
So comparatively delegates add a safety dimension in handling function pointers in .NET.
So we can say that 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 much 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:
Single cast delegate
A delegate is called single cast delegate if it invokes a single method. In other words we can say that SingleCast Delegates refer to a single method with matching signature. SingleCast Delegates derive from the System.Delegate class
SingleCast Delegates can be defined using the following syntax:
public delegate int mySingleCastDelegate int iFirstargument, int iSecondArgument);Single cast delegate example:
using System;
namespace SingleCastDelegateDemoApplication
{
class mySingleDelegateClass
{
public delegate int mySingleCastDelegate(int iFirstargument,
int iSecondArgument);
static void Main(string[] args)
{
mySingleDelegateClass clsSingleCastDelegate = new mySingleDelegateClass();
mySingleCastDelegate singleCastMaxNumberDelegate =
new mySingleCastDelegate(clsSingleCastDelegate.myMaxFunction);
int iMaxNumberResult = singleCastMaxNumberDelegate(10, 20);
Console.WriteLine(@"Result by calling the myMaxFunction method using a
delegate: {0}", iMaxNumberResult);
Console.Read();
}
public int myMaxFunction(int iFirstNumber, int iSecondNumber)
{
if (iFirstNumber > iSecondNumber)
return iFirstNumber;
else
return iSecondNumber;
}
}
}
Multicast Delegates:
MultiCast Delegates are nothing but a single delegate that can invoke multiple methods of matching signature. MultiCast Delegate derives from System.MulticastDelegate class which is a subclass of System.Delegate.
In Multi-Casting basically we create a single delegate that in turn invokes multiple encapsulated methods. We can use MultiCast Delegates when multiple calls to different methods are required. For example if we are required to call two methods on a single button click event or mouse over event then using MultiCast Delegates we can easily call the methods.
The System.MulticastDelegate class provides required methods to play with delegates. There are two methods, Combine and Remove, which are used to play with delegates.
The Combine method is a static method of System.MulticastDelegate class which is used to combine the delegates and the Remove method is used to remove the delegate from the list.
The Combine method takes an array of delegate as a parameter and returns a new delegate that represents the combination of all the delegates in the array.
MultiCast Delegate can have arguments and can have return values as well. The only things is that the methods pointed by delegate needs to have same return type as that of the return type of delegate.
In the below code snippet I have defined a delegate which can act as a multicast delegate to encapsulate methods that takes two parameter as of type integer and returns nothing.
public delegate void MyMulticastDelegate(int p, int q);
Multi cast delegate example:
using System;
namespace MultiCastDelegatesDemoapplication
{
class MultiCastDelegatesClass
{
//delegate declaration
Public delegate void MyMulticastDelegate(int p, int q);
static void Main(string[] args)
{
MultiCastDelegatesClass clsMultiCastDelegate =
new MultiCastDelegatesClass();
MyMulticastDelegate myDelegate = null;
MyMulticastDelegate myMultiCastDelegateAddition =
new MyMulticastDelegate(clsMultiCastDelegate.myAddtionfunction);
MyMulticastDelegate myMultiCastDelegateMaxNumber =
new MyMulticastDelegate(clsMultiCastDelegate.myMaxFunction);
Console.WriteLine("Used Combine function to bind the delegates... ");
myDelegate = (MyMulticastDelegate)System.Delegate.Combine(myMultiCastDelegateAddition,
myMultiCastDelegateMaxNumber);
//call made using the multicast delegate
myDelegate(10, 23);
Console.WriteLine();
Console.WriteLine(@"Used Remove function to remove the delegate from
the list.. ");
myDelegate = (MyMulticastDelegate)System.Delegate.Remove(myDelegate,
myMultiCastDelegateMaxNumber);
myDelegate(10, 23);
Console.Read();
}
//function calculates the sum of passed arguments
public void myAddtionfunction(int iFirstNumber, int iSecondNumber)
{
int result = iFirstNumber + iSecondNumber;
Console.WriteLine(@"In myAddtionfunction called by a multi cast delegate:
Result is :" + result);
}
//function checks the max value of passed arguments
public void myMaxFunction(int iFirstNumber, int iSecondNumber)
{
int max ;
if (iFirstNumber > iSecondNumber)
{
max = iFirstNumber;
}
else
{
max = iSecondNumber;
}
Console.WriteLine(@"In myMaxFunction called by a multi cast delegate:
Result is :" + max);
}
}
}
Add a comment