Multicast Delegates (Multicasting)
Multicast Deletages |
int i, j;
for(j=0, i= sTestString.Length-1; i
>= 0; i--, j++)
temp += sTestString[i];
sTestString = temp;
Console.WriteLine("Reversed
string.");
}
static void Main()
{
// Construct delegates
ModifyString oModifyString;
ModifyString
oModifyStringReplaceSpaces = ReplaceSpaces;
// Method Group Conversion
ModifyString
oModifyStringRemoveSpaces = RemoveSpaces;
// Method Group Conversion
ModifyString
oModifyStringReverseString = Reverse;
// Method Group Conversion
string sString = "This is a test for
Multicast Delegates";
// Set up multicast
oModifyString =
oModifyStringReplaceSpaces;
oModifyString +=
oModifyStringReverseString;
// Call multicast
oModifyString(ref sString);
Console.WriteLine("Resulting
string: " + sString);
Console.WriteLine();
// Remove replace and add remove
oModifyString -=
oModifyStringReplaceSpaces;
oModifyString +=
oModifyStringRemoveSpaces;
sString = "This is a
test."; // reset string
// Call multicast
oModifyString(ref sString);
Console.WriteLine("Resulting
string: " + sString);
Console.WriteLine();
}
}
Why are delegates used?
All delegates are classes that are implicitly derived from System.Delegate.
In general, delegates are useful for two main reasons:
1. Delegates support events.
2. Delegates give your program a way to execute
methods at runtime without having to know precisely what those methods are at
compile time. This ability is quite useful when you want to create a framework
that allows components to be plugged in
Definition, what is Multicast Deletages (Multicasting),
real world scenarios and live code example of Multicast Deletages
(Multicasting), use of Multicast Deletages (Multicasting)