多播委托和简单委托之间的区别?

时间:2014-04-16 08:34:37

标签: c# delegates

我的理解是委托只指向一种方法。

如果通过多次转换委托A指向方法B和方法C,为什么我们不能委托A指向方法B而方法B指向方法C?

3 个答案:

答案 0 :(得分:1)

  

如果通过多次转换委托A指向方法B和方法C,为什么我们不能让委托A指向方法B而方法B指向方法C?

当然,你可以做到。但代表的全部意义在于委托使用者不需要知道实现是什么。在具有多个目标的代表中,关键点在于他们不需要了解彼此。由于事件是由代表实现的,因此事件通常有多个订阅者,这可能是具有多个目标的代理的主要用法。

但是,在.NET中,所有代理都支持多个目标,因此它没有实际意义。

撇开;在你的A / B / C场景中,你也可以这样做:

SomeDelegateType instance = delegate {
    B();
    C();
};

这是一个调用BC的代理人。或者你可以这样做:

SomeDelegateType instance = new SomeDelegateType(B) + new SomeDelegateType(C);

或者你想要的任何东西,真的。这无关紧要。

答案 1 :(得分:0)

delegate void Del(string s);

class TestClass
{
static void Hello(string s)
{
    System.Console.WriteLine("  Hello, {0}!", s);
}

static void Goodbye(string s)
{
    System.Console.WriteLine("  Goodbye, {0}!", s);
}

static void Main()
{
    Del a, b, c, d;

    // Create the delegate object a that references 
    // the method Hello:
    a = Hello;

    // Create the delegate object b that references 
    // the method Goodbye:
    b = Goodbye;

    // The two delegates, a and b, are composed to form c: 
    c = a + b;

    // Remove a from the composed delegate, leaving d, 
    // which calls only the method Goodbye:
    d = c - a;

    System.Console.WriteLine("Invoking delegate a:");
    a("A");
    System.Console.WriteLine("Invoking delegate b:");
    b("B");
    System.Console.WriteLine("Invoking delegate c:");
    c("C");
    System.Console.WriteLine("Invoking delegate d:");
    d("D");
}
}
 /* Output:
Invoking delegate a:
Hello, A!
Invoking delegate b:
Goodbye, B!
Invoking delegate c:
Hello, C!
Goodbye, C!
Invoking delegate d:
Goodbye, D!
 */

答案 2 :(得分:0)

定义多播委托时,表示您可以向该委托添加所需数量的任务。并且调用此多播委托将调用所指向的所有函数。 从MSDN

中查看此示例
using System;

// Define a custom delegate that has a string parameter and returns void. 
delegate void CustomDel(string s);

class TestClass
{
    // Define two methods that have the same signature as CustomDel. 
    static void Hello(string s)
    {
        System.Console.WriteLine("  Hello, {0}!", s);
    }

    static void Goodbye(string s)
    {
        System.Console.WriteLine("  Goodbye, {0}!", s);
    }

    static void Main()
    {
        // Declare instances of the custom delegate.
        CustomDel hiDel, byeDel, multiDel, multiMinusHiDel;

        // In this example, you can omit the custom delegate if you  
        // want to and use Action<string> instead. 
        //Action<string> hiDel, byeDel, multiDel, multiMinusHiDel; 

        // Create the delegate object hiDel that references the 
        // method Hello.
        hiDel = Hello;

        // Create the delegate object byeDel that references the 
        // method Goodbye.
        byeDel = Goodbye;

        // The two delegates, hiDel and byeDel, are combined to  
        // form multiDel. 
        multiDel = hiDel + byeDel;

        // Remove hiDel from the multicast delegate, leaving byeDel, 
        // which calls only the method Goodbye.
        multiMinusHiDel = multiDel - hiDel;

        Console.WriteLine("Invoking delegate hiDel:");
        hiDel("A");
        Console.WriteLine("Invoking delegate byeDel:");
        byeDel("B");
        Console.WriteLine("Invoking delegate multiDel:");
        multiDel("C");
        Console.WriteLine("Invoking delegate multiMinusHiDel:");
        multiMinusHiDel("D");
    }
}

/* Output:
Invoking delegate hiDel:
  Hello, A!
Invoking delegate byeDel:
  Goodbye, B!
Invoking delegate multiDel:
  Hello, C!
  Goodbye, C!
Invoking delegate multiMinusHiDel:
  Goodbye, D!
*/`

希望它有所帮助。