Demonstrate multicasting : Multi cast delegate « delegate « C# / CSharp Tutorial






Create an invocation list, or chain, of methods that will be called when a delegate is invoked.

using System; 
 
delegate string StrMod(ref string str); 
 
class MultiCastDemo { 
  static string replaceSpaces(ref string a) { 
    Console.WriteLine("replaceSpaces"); 
    return a; 
  }  
 
  static string removeSpaces(ref string a) { 
    Console.WriteLine("removeSpaces"); 
    return a; 
  }  
 
  static string reverse(ref string a) { 
    Console.WriteLine("reverseSpaces"); 
    return a; 
  } 
     
  public static void Main() {  
    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; 
 
    // Call multicast. 
    strOp(ref str); 
     
    // Remove replace and add remove. 
    strOp -= replaceSp; 
    strOp += removeSp; 
 
    str = "This is a test."; // reset string 
 
    // Call multicast. 
    strOp(ref str); 
  } 
}
replaceSpaces
reverseSpaces
reverseSpaces
removeSpaces








9.2.Multi cast delegate
9.2.1.Demonstrate multicasting
9.2.2.Multicast delegates
9.2.3.Remove delegate from Multicast
9.2.4.uses the reference parameter of a multicast delegate as a counter