Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 简单委托(委托)与多播委托_C#_.net_Delegates_Multicastdelegate - Fatal编程技术网

C# 简单委托(委托)与多播委托

C# 简单委托(委托)与多播委托,c#,.net,delegates,multicastdelegate,C#,.net,Delegates,Multicastdelegate,我已经阅读了很多文章,但是我仍然不清楚我们通常创建的普通委托和多播委托之间的区别 public delegate void MyMethodHandler(object sender); MyMethodHandler handler = new MyMethodHandler(Method1); handler += Method2; handler(someObject); 上面的委托MyMethodHandler将调用这两个方法。 现在,多播代理从何而来。我读到他们可以调用多种方法,但我

我已经阅读了很多文章,但是我仍然不清楚我们通常创建的普通委托和多播委托之间的区别

public delegate void MyMethodHandler(object sender);
MyMethodHandler handler = new MyMethodHandler(Method1);
handler += Method2;
handler(someObject);
上面的委托MyMethodHandler将调用这两个方法。 现在,多播代理从何而来。我读到他们可以调用多种方法,但我担心我对代理的基本理解不正确

很好地解释了这一点:

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!
*/
C#规范规定,所有委托类型必须可转换为
System.delegate
。实际上,实现实现这一点的方式是,所有委托类型都派生自
System.MulticastDelegate
,后者又派生自
System.delegate

清楚了吗?我不确定这是否回答了您的问题。

“所有代理实例都具有多播功能。”


“在C#中,所有委托类型都支持多播”-

很抱歉添加到其他人的答案中,但我认为委托是按照添加顺序调用的

“多播代理”部分

澄清一点:所有委托都是类
MulticastDelegate
的实例,不管它们是有一个还是多个目标方法。原则上,具有单个或多个目标的委托之间没有区别,尽管运行时针对具有单个目标的常见情况进行了一些优化。(但目标为0的委托是不可能的,它是一个或多个。)

当您实例化一个委托(如
newmymethodhandler(Method1)
)时,您将创建一个具有单个目标的委托(即
Method1
方法)

通过组合两个现有代理来创建具有多个目标的代理。生成的委托将具有目标的总和。委托可以显式地与
Delegate.Combine()
组合,但也可以通过对现有委托使用
+=
运算符隐式组合,如您的示例所示


依次调用委托会调用委托中的每个目标。因此,在您的示例中,
处理程序(someObject)
将调用两个方法(
Method1
Method2
),因为您已经创建了一个具有这两个目标的委托。

多播委托是一个引用了多个函数的委托。调用多播委托时,将调用委托指向的所有函数

类型1:

0参数和无效返回类型委托-

using System;

delegate void SampleDelegate (ref int SampleReferenceParameter);

class MainClass
{
    public static void Main ()
    {
        SampleDelegate del1, del2, del3, del4;
        del1 = new SampleDelegate (SampleMethodOne);
        del2 = new SampleDelegate (SampleMethodTwo);
        del3 = new SampleDelegate (SampleMethodTwo);
        del4 = del1 + del2 + del3 - del3;

        int SampleReferenceParameterValue = 0;
        del4 (ref SampleReferenceParameterValue);

        Console.WriteLine (SampleReferenceParameterValue); 
    }

    public static void SampleMethodOne (ref int SampleReferenceParameter)
    {
        SampleReferenceParameter = 1;
    }

    public static void SampleMethodTwo (ref int SampleReferenceParameter)
    {
        SampleReferenceParameter = 2;
    }

    public static void SampleMethodThree (ref int SampleReferenceParameter)
    {
        SampleReferenceParameter = 3;
    }
}

/*
Here del4 is first set as sum of del1, del2 and del3. Then del3 is subtracted from del4. So del4 now has del1, del2.

When del4 is invoked, first del1 and then del2 is invoked.

del1 sets reference parameter to 1. del2 sets reference parameter to 2.

But since del2 is called last final value of reference parameter is 2
*/
方法1-

using System;

delegate void SampleDelegate ();    //A delegate with 0 argument and void     return type is declared

class MainClass
{
    public static void Main ()
    {
        SampleDelegate Del1 = new SampleDelegate (Message1);         //Del1 declared which points to function Message1
        SampleDelegate Del2 = new SampleDelegate (Message2);        //Del2 declared which points to function Message2
        SampleDelegate Del3 = new SampleDelegate (Message3);        //Del3 declared which points to function Message3
        SampleDelegate Del4 = Del1 + Del2 + Del3;                   //Del4 declared which points to function Message4

        //Del4 is then initialized as sum of Del1 + Del2 + Del3

        Del4 ();        //Del4 is invoked;

        //Del4 in turn invokes Del1, Del2 and Del3 in the same order they were initialized to Del4
        //Del1, Del2, Del3 in turn invokes their respective functions to which they point to
        //The three functions Message1, Message2 and Message3 gets executed one after another

    }

        //Output:
        //This is message 1
        //This is message 2
        //This is message 3

        Del4 - Del1;    //Removes Del1 from Del4
        Del4();           

        //New Output:
        //This is message 2
        //This is message 3

        Del4 + Del1;    //Again adds Del1 to Del4
        Del4();

        //New Output:
        //This is message 1
        //This is message 2
        //This is message 3


    public static void Message1 ()      //First sample function matching delegate signature
    {
        Console.WriteLine ("This is message 1");
    }

    public static void Message2 ()      //Second sample function
    {
         Console.WriteLine ("This is message 2");
    }

    public static void Message3 ()      //Third sample function
    {
        Console.WriteLine ("This is message 3");
    }
}
using System;

delegate int SampleDelagate ();

class MainClass
{
    public static void Main ()
   {
        SampleDelagate del1 = new SampleDelagate (Method1);
        SampleDelagate del2 = new SampleDelagate (Method2);
        SampleDelagate del3 = new SampleDelagate (Method3);
        SampleDelagate del4 = del1 + del2 + del3;

        int ValueReturned = del4 ();

        //Del4 invokes Del1, Del2, Del3 in the same order. Here the return type is int. So the return of last delegate del3 is returned. Del3 points to Method3. So returned value is 3.

        Console.WriteLine (ValueReturned);

        //Output: 3
    }

    public static int Method1 ()
    {
        return 1;
    }

    public static int Method2 ()
    {
        return 2;
    }

    public static int Method3 ()
    {
        return 3;
    }
}
方法2-

using System;

delegate void SampleDelegate ();

class MainClass
{
    public static void Main ()
    {
        SampleDelegate del = new SampleDelegate (Message1);         //Declares del and initializes it to point to method Message1
        del += Message2;                                        //Now method Message2 also gets added to del. Del is now pointing to two methods, Message1 and Message2. So it is now a MultiCast Delegate
        del += Message3;                                        //Method Message3 now also gets added to del

        del ();                                                 //Del invokes Message1, Message2 and Message3 in the same order as they were added

        /*
        Output:
        This is Message1
        This is Message2
        This is Message3
        */

        del -= Message1;                                        //Method     Message1 is now removed from Del. It no longer points to Message1
                                                                //Del invokes the two remaining Methods Message1 and Message2 in the same order
        del ();
        /*
        New Output:
        This is Message2
        This is Message3
        */

        del += Message4;                                        //Method Message4 gets added to Del. The three Methods that Del oints to are in the order 1 -> Message2, 2 -> Message3, 3 -> Message4
                                                                //Del invokes the three methods in the same order in which they are present.
        del ();
        /*
        New Output:
        This is Message2
        This is Message3
        This is Message4
        */

    }

    public static void Message1 ()
    {
        Console.WriteLine ("This is Message1");
    }

    public static void Message2 ()
    {
        Console.WriteLine ("This is Message2");
    }

    public static void Message3 ()
    {
        Console.WriteLine ("This is Message3");
    }

    public static void Message4 ()
    {
        Console.WriteLine ("This is Message4");
    }
}
类型2:

0参数和int返回类型委托

方法1-

using System;

delegate void SampleDelegate ();    //A delegate with 0 argument and void     return type is declared

class MainClass
{
    public static void Main ()
    {
        SampleDelegate Del1 = new SampleDelegate (Message1);         //Del1 declared which points to function Message1
        SampleDelegate Del2 = new SampleDelegate (Message2);        //Del2 declared which points to function Message2
        SampleDelegate Del3 = new SampleDelegate (Message3);        //Del3 declared which points to function Message3
        SampleDelegate Del4 = Del1 + Del2 + Del3;                   //Del4 declared which points to function Message4

        //Del4 is then initialized as sum of Del1 + Del2 + Del3

        Del4 ();        //Del4 is invoked;

        //Del4 in turn invokes Del1, Del2 and Del3 in the same order they were initialized to Del4
        //Del1, Del2, Del3 in turn invokes their respective functions to which they point to
        //The three functions Message1, Message2 and Message3 gets executed one after another

    }

        //Output:
        //This is message 1
        //This is message 2
        //This is message 3

        Del4 - Del1;    //Removes Del1 from Del4
        Del4();           

        //New Output:
        //This is message 2
        //This is message 3

        Del4 + Del1;    //Again adds Del1 to Del4
        Del4();

        //New Output:
        //This is message 1
        //This is message 2
        //This is message 3


    public static void Message1 ()      //First sample function matching delegate signature
    {
        Console.WriteLine ("This is message 1");
    }

    public static void Message2 ()      //Second sample function
    {
         Console.WriteLine ("This is message 2");
    }

    public static void Message3 ()      //Third sample function
    {
        Console.WriteLine ("This is message 3");
    }
}
using System;

delegate int SampleDelagate ();

class MainClass
{
    public static void Main ()
   {
        SampleDelagate del1 = new SampleDelagate (Method1);
        SampleDelagate del2 = new SampleDelagate (Method2);
        SampleDelagate del3 = new SampleDelagate (Method3);
        SampleDelagate del4 = del1 + del2 + del3;

        int ValueReturned = del4 ();

        //Del4 invokes Del1, Del2, Del3 in the same order. Here the return type is int. So the return of last delegate del3 is returned. Del3 points to Method3. So returned value is 3.

        Console.WriteLine (ValueReturned);

        //Output: 3
    }

    public static int Method1 ()
    {
        return 1;
    }

    public static int Method2 ()
    {
        return 2;
    }

    public static int Method3 ()
    {
        return 3;
    }
}
方法2-

using System;

delegate void SampleDelegate ();    //A delegate with 0 argument and void     return type is declared

class MainClass
{
    public static void Main ()
    {
        SampleDelegate Del1 = new SampleDelegate (Message1);         //Del1 declared which points to function Message1
        SampleDelegate Del2 = new SampleDelegate (Message2);        //Del2 declared which points to function Message2
        SampleDelegate Del3 = new SampleDelegate (Message3);        //Del3 declared which points to function Message3
        SampleDelegate Del4 = Del1 + Del2 + Del3;                   //Del4 declared which points to function Message4

        //Del4 is then initialized as sum of Del1 + Del2 + Del3

        Del4 ();        //Del4 is invoked;

        //Del4 in turn invokes Del1, Del2 and Del3 in the same order they were initialized to Del4
        //Del1, Del2, Del3 in turn invokes their respective functions to which they point to
        //The three functions Message1, Message2 and Message3 gets executed one after another

    }

        //Output:
        //This is message 1
        //This is message 2
        //This is message 3

        Del4 - Del1;    //Removes Del1 from Del4
        Del4();           

        //New Output:
        //This is message 2
        //This is message 3

        Del4 + Del1;    //Again adds Del1 to Del4
        Del4();

        //New Output:
        //This is message 1
        //This is message 2
        //This is message 3


    public static void Message1 ()      //First sample function matching delegate signature
    {
        Console.WriteLine ("This is message 1");
    }

    public static void Message2 ()      //Second sample function
    {
         Console.WriteLine ("This is message 2");
    }

    public static void Message3 ()      //Third sample function
    {
        Console.WriteLine ("This is message 3");
    }
}
using System;

delegate int SampleDelagate ();

class MainClass
{
    public static void Main ()
   {
        SampleDelagate del1 = new SampleDelagate (Method1);
        SampleDelagate del2 = new SampleDelagate (Method2);
        SampleDelagate del3 = new SampleDelagate (Method3);
        SampleDelagate del4 = del1 + del2 + del3;

        int ValueReturned = del4 ();

        //Del4 invokes Del1, Del2, Del3 in the same order. Here the return type is int. So the return of last delegate del3 is returned. Del3 points to Method3. So returned value is 3.

        Console.WriteLine (ValueReturned);

        //Output: 3
    }

    public static int Method1 ()
    {
        return 1;
    }

    public static int Method2 ()
    {
        return 2;
    }

    public static int Method3 ()
    {
        return 3;
    }
}
与类型1相同的过程

因此,当存在多播委托的返回类型时,返回值是最后一个委托的返回值

类型3:

int、int、ref int参数和void返回类型委托-

using System;

delegate void SampleDelegate (ref int SampleReferenceParameter);

class MainClass
{
    public static void Main ()
    {
        SampleDelegate del1, del2, del3, del4;
        del1 = new SampleDelegate (SampleMethodOne);
        del2 = new SampleDelegate (SampleMethodTwo);
        del3 = new SampleDelegate (SampleMethodTwo);
        del4 = del1 + del2 + del3 - del3;

        int SampleReferenceParameterValue = 0;
        del4 (ref SampleReferenceParameterValue);

        Console.WriteLine (SampleReferenceParameterValue); 
    }

    public static void SampleMethodOne (ref int SampleReferenceParameter)
    {
        SampleReferenceParameter = 1;
    }

    public static void SampleMethodTwo (ref int SampleReferenceParameter)
    {
        SampleReferenceParameter = 2;
    }

    public static void SampleMethodThree (ref int SampleReferenceParameter)
    {
        SampleReferenceParameter = 3;
    }
}

/*
Here del4 is first set as sum of del1, del2 and del3. Then del3 is subtracted from del4. So del4 now has del1, del2.

When del4 is invoked, first del1 and then del2 is invoked.

del1 sets reference parameter to 1. del2 sets reference parameter to 2.

But since del2 is called last final value of reference parameter is 2
*/

多播委托仅仅是在其调用列表中具有多个方法引用的普通委托吗?没错。一个多播委托将调用多个方法。NET中的委托是多播委托(据我所知)。无论您选择将零个或一个或多个处理程序附加到它们,它们仍然是多播委托。需要注意的是,多播委托将以不可预测的顺序调用其订阅者。不要假设它们将以任何特定的顺序被调用。@MikeChristian获得了“多播委托将以不可预测的顺序调用其订户”的链接?书的第二段似乎说它是按顺序排列的。是的,埃里克。通常,当我向人们(或人们问我)询问代理时,我们通常会说有两种类型的代理:单播和多播。现在我知道“委托”这样的东西只有一种,它可以是单播的,也可以是多播的,这取决于它包含的方法引用的数量。任何委托实例如何能够调用除创建它要执行的操作之外的任何数量的操作?如果创建委托是为了调用三件事,我认为委托实例将始终执行三件事。同样,如果它是为了做一件事而创建的。如果一个委托被创建为只做一件事,那么这个实例怎么能做得更多呢?@supercat,它没有。那么“所有委托实例都有多播功能”是什么意思?当然,所有委托类型都有这样的能力,并且必须准备好接受除事件订阅之外的任何委托的代码,使其成为多播委托(如果事件在内部使用多播委托,则将多播委托传递给
Add
方法,然后再将其传递给
Remove
可能实际上不会取消订阅该事件;默认事件处理程序以这种方式出现故障意味着事件预计不会容忍多播事件).Yup,并返回上一个函数的结果(如果它具有返回类型-怪异)。