Creating Multicast Delegates


A multicast delegate is a delegate that can invoke multiple methods, so long as the methods all have the same signature as the delegate's definition. Multicast delegates are the steppingstone between standard delegates and the use of events. When you understand both delegates and multicast delegates, you will be ready to start working with events.

You create a multicast delegate through composition. Composing multiple delegates is accomplished with the +, -, +=, and -= operators. To make a delegate invoke multiple methods, you simply add the method to an existing delegate using + or +=. To remove a method from a multicast delegate, you use the or the -= overloaded operators.

The code in Listing 9.3 provides an illustration of how to create and invoke multicast delegates. In addition, the sample also shows creating a multicast delegate from both named and anonymous methods, and how to iterate through the list of methods that will be invoked by a multicast delegate.

Listing 9.3. Multicast Delegate Sample

using System; using System.Collections.Generic; using System.Text; namespace MulticastDemo { class Program { delegate void MessagePrintDelegate(string s); static void Main(string[] args) {     MessagePrintDelegate mpd1, mpd2, mpd3, mpd4;     mpd1 = PrintMessage;     mpd2 = PrintStampedMessage;     // create a multicast by composing two delegates     mpd3 = mpd1 + mpd2;     mpd3("Hello World");     mpd4 = PrintMessage;     mpd4 += mpd3;     mpd4("Multicasted!");     // you can also remove a delegate from a composition     mpd3 -= mpd1;     mpd3("Hello World (again)");     // you can compose 2 multicasts     mpd4 += mpd3;     mpd4("Really multicasted");     // you can even add an anonymous method to the multicast     mpd3 += delegate(string s)     {         Console.WriteLine("[Anonymous] {0}", s);     };     mpd3("Hello!");     // you can inspect the list of methods     // in any given multicast     Console.WriteLine("Methods in the mpd3 multicast:");     foreach (Delegate d in mpd3.GetInvocationList())     {         Console.WriteLine("\t{0}", d.Method.Name);     }     Console.ReadLine(); } static void PrintMessage(string s) {     Console.WriteLine(s); } static void PrintStampedMessage(string s) {     Console.WriteLine("[{0}] {1}", DateTime.Now.ToShortTimeString(),         s); } } } 

When you run the preceding code, the output looks as follows, illustrating how a single invocation to a multicast delegate can invoke the target of that delegate multiple times:

Hello World [8:50 PM] Hello World Multicasted! Multicasted! [8:50 PM] Multicasted! [8:50 PM] Hello World (again) Really multicasted Really multicasted [8:50 PM] Really multicasted [8:50 PM] Really multicasted [8:50 PM] Hello! [Anonymous] Hello! Methods in the mpd3 multicast:         PrintStampedMessage         <Main>b__0 


As you can see, multicast delegates are an extremely powerful feature with virtually unlimited applications. Events, which are one of those applications, are discussed next. It is also worth noting that in the preceding output, the anonymous method has been assigned a name by the CLR: <Main>b__0. All methods must be identified by a name, even anonymous methods, so the CLR generates a name dynamically based on the scope and sequence of the method.



Microsoft Visual C# 2005 Unleashed
Microsoft Visual C# 2005 Unleashed
ISBN: 0672327767
EAN: 2147483647
Year: 2004
Pages: 298

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net