Multicast delegates

Invoking more than one method through a delegate is called multicasting. You can use +-+=, or -+ to add or remove methods from the list of invoking methods. This list is called the invocation list. It's used in event handling.

The following example shows how we can invoke multiple methods by invoking a delegate. We have two methods, both of which accept a string parameter and display it on the screen. In the multicast delegate method, we are associating two methods with stringdelegate

delegate void StringDelegate( string strVariable);
public void MulticastDelegate()
{
StringDelegate StringDel = HelperClass.StringMethod;
StringDel += HelperClass.StringMethod2;
StringDel("Chapter 5 - Multicast delegate Method1");
}

//Helper Class Methods
public static void StringMethod(string s)
{
Console.WriteLine(s);
}

public static void StringMethod2(string s)
{
Console.WriteLine("Method2 :" + s);
}


/Output:
Chapter 5 - Multicast delegate Method1
Method2 :Chapter 5 - Multicast delegate Method1
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.142.85