The following C sharp example depicts will you the method to compose multicast delegates. A useful property of multicast delegate objects is that they can be assigned to single delegate instance using the + operator.
Example:
Code:
delegate void Del1(string s1);
class TestClass1
{
static void Hello(string s1)
{
System.Console.WriteLine(" Hello, {0}!", s);
}
static void Goodbye(string s1)
{
System.Console.WriteLine(" Goodbye, {0}!", s);
}
static void Main()
{
Del a1, b1, c1, d1;
a1 = Hello;
// Create the delegate object b that references
b1 = Goodbye;
// The two delegates, a and b, are composed to form c1:
c1 = a1 + b1;
// Remove a from the composed delegate, leaving d1,
d1 = c1 - a1;
System.Console.WriteLine("Invoking delegate a1:");
a("A1");
System.Console.WriteLine("Invoking delegate b1:");
b("B1");
System.Console.WriteLine("Invoking delegate c1:");
c("C1");
System.Console.WriteLine("Invoking delegate d1:");
d("D1");
}
}
Bookmarks