Multicast delegate C#

后端 未结 3 1592
栀梦
栀梦 2021-02-10 16:20

Am studying about delegates. As I read. I learned that adding more than one function in a delegate is called multicast delegate. Based on that I wrote a program. Here two functi

3条回答
  •  误落风尘
    2021-02-10 17:06

    Actually all delegates in C# are MulticastDelegates, even if they only have a single method as target. (Even anonymous functions and lambdas are MulticastDelegates even though they by definition have only single target.)

    MulticastDelegate is simply the base class for all kinds of function or method references in C#, whether they contain one or more targets.

    So this:

    MyDelegate myDel = new MyDelegate(AddNumbers);
    

    Sets myDel to a MulticastDelegate with a single target. But this line:

    myDel += new MyDelegate(MultiplyNumbers);
    

    Updates myDel to a MulticastDelegate with two targets.

提交回复
热议问题