Multicasting

One of the most exciting features of a delegate is its support for multicasting.

Multicasting is the ability to create a chain of methods that will be automatically called when a delegate is invoked. Such a chain is very easy to create.

Simply instantiate a delegate, and then use the += operator to add methods to the chain. To remove a method, use – =. (You can also use the +, –, and = operators separately to add and subtract delegates, but += and – = are

more convenient.)

The only restriction is that the delegate being multicast must have a void return type.

Here is an example of multicasting. It reworks the preceding examples by

changing the string manipulation method’s return type to void, and using a ref

parameter to return the altered string to the caller.

// Demonstrate multicasting.

using System;

// Declare a delegate.

delegate void strMod(ref string str);

class StringOps {

// Replaces spaces with hyphens.

static void replaceSpaces(ref string a) {

Console.WriteLine(“Replaces spaces with hyphens.”);

a = a.Replace(‘ ‘, ‘-‘);

}

// Remove spaces.

static void removeSpaces(ref string a) {

string temp = “”;int i;

Console.WriteLine(“Removing spaces.”);

for(i=0; i < a.Length; i++)

if(a[i] != ‘ ‘) temp += a[i];

a = temp;

}

// Reverse a string.

static void reverse(ref string a) {

string temp = “”;

int i, j;

Console.WriteLine(“Reversing string.”);

for(j=0, i=a.Length-1; i >= 0; i–, j++)

temp += a[i];

a = temp;

}

public static void Main() {

// Construct delegates.

strMod strOp;

strMod replaceSp = new strMod(replaceSpaces);

strMod removeSp = new strMod(removeSpaces);

strMod reverseStr = new strMod(reverse);

string str = “This is a test”;

// set up multicast

strOp = replaceSp;

strOp += reverseStr; <——————————-Create a multicast.

// Call multicast

strOp(ref str);

Console.WriteLine(“Resulting string: ” + str);

Console.WriteLine();

// remove replace and add remove

strOp -= replaceSp;

strOp += removeSp; <————————-Create a different multicast.

str = “This is a test.”; // reset string

// Call multicast

strOp(ref str);

Console.WriteLine(“Resulting string: ” + str);

Console.WriteLine();

}

}

Here is the output:

Replaces spaces with hyphens.

Reversing string.

Resulting string: tset-a-si-sihT

Reversing string.

Removing spaces.

Resulting string: .tsetasisihT

In Main( ), four delegate instances are created. One, strOp, is null. The other three refer to specific string modification methods. Next, a multicast is created that calls removeSpaces( ) and reverse( ). This is accomplished via the following lines:

strOp = replaceSp;

strOp += reverseStr;

First, strOp is assigned a reference to replaceSp. Next, using +=, reverseStr is

added. When strOp is invoked, both methods are invoked, replacing spaces with hyphens and reversing the string, as the output illustrates.

Next, replaceSp is removed from the chain, using this line:

strOp -= replaceSp;

and removeSp is added using this line:

strOp += removeSp;

Then, StrOp is again invoked. This time, spaces are removed and the string is reversed.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: