Introduction
Multicast delegates provide functionality to execute more than one method.
Internally, a linked list of delegates (called Invocation List) is stored, and when the multicast delegate is invoked, the list of delegates will be executed in sequence.
How to…
The simplest way to start working with multicast delegates is to create single delegates and combine them to a multicast delegate, as described in the following code snippet:
public delegate void myDel();
static public void Main(string[] args)
{
myDel myDelA = new myDel(MethodA);
myDel myDelB = new myDel(MethodB);
myDel myMultiCast = (myDel)Delegate.Combine(myDelA, myDelB);
myMultiCast.Invoke();
}
static void MethodA()
{
Console.WriteLine(“Executing method A.”);
}
static void MethodB()
{
Console.WriteLine(“Executing method B.”);
}
Here is the output:
Executing method A.
Executing method B.
Deriving a class from the MulticastDelegate class
The Delegate
and MulticastDelegate
classes cannot be derived explicitly, but there’s a way to do that.
The following example defines three classes named Order
, Stock
, and Receipt
beyond the main method, from a console application, for example:
- The
Order
class holds the delegate that defines the methods signature, and provides methods to add product items to the order, and a checkout method that will use a delegate as a parameter to define what method to call. It can be used for single delegates or a multicast delegate.
- The
Stock
class has an operation to remove the products from the stock.
- The
Receipt
class has an operation to print an item to the receipt.
- The
Main
method creates an instance of the Order
type, adding product items to the order, and creates the multicast delegate based on a combination of two derived delegates.
Let’s check the code:
class Order
{
public delegate void myOrderDel(int prodId, int quantity);
private static HybridDictionary prodList = new HybridDictionary();
public void AddItem(int prodId, int quantity)
{
prodList.Add(prodId, quantity);
}
public static void Checkout(myOrderDel multicastDelegate)
{
foreach (DictionaryEntry prod in prodList)
{
multicastDelegate.Invoke(Convert.ToInt32(prod.Key),
Convert.ToInt32(prod.Value));
}
}
}
class Stock
{
public static void Remove(int prodId, int quantity)
{
Console.WriteLine(“{0} unit(s) of the product {1} has/have" +
" been removed from the stock.”, quantity, prodId);
}
}
class Receipt
{
public static void PrintItem(int prodId, int quantity)
{
Console.WriteLine(“{0} unit(s) of the product {1} has/have been" +
" printed to the receipt.”, quantity, prodId);
}
}
static public void Main(string[] args)
{
Order myOrder = new Order();
myOrder.AddItem(1, 2);
myOrder.AddItem(2, 3);
myOrder.AddItem(3, 1);
myOrder.AddItem(4, 1);
myOrder.AddItem(5, 4);
Order.myOrderDel myStockDel = new Order.myOrderDel(Stock.Remove);
Order.myOrderDel myReceiptDel = new Order.myOrderDel(Receipt.PrintItem);
Order.myOrderDel myMulticastDel =
(Order.myOrderDel)Delegate.Combine(myStockDel, myReceiptDel);
Order.Checkout(myMulticastDel);
}
Here is the output:
2 unit(s) of the product 1 has/have been removed from the stock.
2 unit(s) of the product 1 has/have been printed to the receipt.
3 unit(s) of the product 2 has/have been removed from the stock.
3 unit(s) of the product 2 has/have been printed to the receipt.
1 unit(s) of the product 3 has/have been removed from the stock.
1 unit(s) of the product 3 has/have been printed to the receipt.
1 unit(s) of the product 4 has/have been removed from the stock.
1 unit(s) of the product 4 has/have been printed to the receipt.
4 unit(s) of the product 5 has/have been removed from the stock.
4 unit(s) of the product 5 has/have been printed to the receipt.
The MSDN site provides another nice example about how to derive a class (not in an explicit way) from a MulticastDelegate
class. Click here to read!