Multicast Delegate
Delegate that holds the reference of more than one method.
Multicast delegates must contain only methods that return void, otherwise run-time exception will be thrown.
Example
delegate void DelegateMulticast(int x);
Class ClassX
{
static void A(int x)
{
Console.WriteLine("Hello A");
}
static void B(int x)
{
Console.WriteLine("Hello B");
}
public static void Main()
{
DelegateMulticast func = new DelegateMulticast(A);
func += new DelegateMulticast(B);
func(1); // Both A and B are called
func -= new DelegateMulticast(A);
func(2); // Only B is called
}
}
Description
As you see that there are 2 methods named A & B with one parameter and void as return type.
Create delegate object
DelegateMulticast func = new DelegateMulticast(A);
Then the Delegate are added and removed using the += operator and -= operator resp.
Delegate that holds the reference of more than one method.
Multicast delegates must contain only methods that return void, otherwise run-time exception will be thrown.
Example
delegate void DelegateMulticast(int x);
Class ClassX
{
static void A(int x)
{
Console.WriteLine("Hello A");
}
static void B(int x)
{
Console.WriteLine("Hello B");
}
public static void Main()
{
DelegateMulticast func = new DelegateMulticast(A);
func += new DelegateMulticast(B);
func(1); // Both A and B are called
func -= new DelegateMulticast(A);
func(2); // Only B is called
}
}
Description
As you see that there are 2 methods named A & B with one parameter and void as return type.
Create delegate object
DelegateMulticast func = new DelegateMulticast(A);
Then the Delegate are added and removed using the += operator and -= operator resp.
can u xplin y run time exception will be thrown?
ReplyDelete