Csharp/CSharp Tutorial/delegate/Multi cast delegate — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 12:19, 26 мая 2010

Demonstrate multicasting

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

Multicast delegates

using System;
class MainClass
{
  delegate int MyDelegate(string s);
  static void Main(string[] args)
  {
    string MyString = "Hello World";
    
    MyDelegate Multicast = null;
    Multicast += new MyDelegate(DoSomething);
    Multicast += new MyDelegate(DoSomething2);
    Multicast(MyString);
  }
  static int DoSomething(string s)
  {
    Console.WriteLine("DoSomething");
    
    return 0;
  }
  static int DoSomething2(string s)
  {
      Console.WriteLine("DoSomething2");
    return 0;
  }
}
DoSomething
DoSomething2

Remove delegate from Multicast

using System;
class MainClass
{
  delegate int MyDelegate(string s);
  static void Main(string[] args)
  {
    string MyString = "Hello World";
    //Or you can Multicast delegates by doing this
    MyDelegate Multicast = null;
    Multicast += new MyDelegate(DoSomething);
    Multicast += new MyDelegate(DoSomething2);
    Multicast -= new MyDelegate(DoSomething2);

    Multicast(MyString);
  }
  static int DoSomething(string s)
  {
    Console.WriteLine("DoSomething");
    
    return 0;
  }
  static int DoSomething2(string s)
  {
      Console.WriteLine("DoSomething2");
    return 0;
  }
}
DoSomething

uses the reference parameter of a multicast delegate as a counter

using System;
public delegate void DelegateClass(int valCount, ref int refCount);
public class Counter {
    public static void Main() {
        DelegateClass del = (DelegateClass)AddOne + (DelegateClass)AddTwo + (DelegateClass)AddOne;
        int valCount = 0;
        int refCount = 0;
        del(valCount, ref refCount);
        Console.WriteLine("Value count = {0}", valCount); // 0
        Console.WriteLine("Reference count = {0}",refCount); // 4
    }
    public static void AddOne(int valCount,ref int refCount) {
        ++valCount;
        ++refCount;
    }
    public static void AddTwo(int valCount,ref int refCount) {
        valCount += 2;
        refCount += 2;
    }
}