Multicast delegate C#
Asked Answered
L

3

5

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 functions (AddNumbers and MultiplyNumbers) I added in the MyDelegate. Is the below program is an example for multicast delegate ?.

public partial class MainPage : PhoneApplicationPage
{
    public delegate void MyDelegate(int a, int b);
    // Constructor
    public MainPage()
    {
        InitializeComponent();

        MyDelegate myDel = new MyDelegate(AddNumbers);
        myDel += new MyDelegate(MultiplyNumbers);
        myDel(10, 20);
    }

    public void AddNumbers(int x, int y)
    {
        int sum = x + y;
        MessageBox.Show(sum.ToString());
    }

    public void MultiplyNumbers(int x, int y)
    {
        int mul = x * y;
        MessageBox.Show(mul.ToString());
    }

}
Lifeanddeath answered 22/2, 2013 at 11:59 Comment(2)
This question might be better suited to Code ReviewFinsteraarhorn
Dupe: simple-delegate-delegate-vs-multicast-delegatesSuccessor
M
10

Yes, it's an example of a multicast delegate. Note that instead of

new MyDelegate(AddNumbers)

you can typically say just

AddNumbers

because a so-called method group conversion exists that will create the delegate instance for you.

Another thing to note is that your declaration public delegate void MyDelegate(int a, int b); does not have to reside inside another type (here inside the MainPage class). It could be a direct member of the namespace (since it's a type). But of course it's perfectly valid to "nest" it inside a class, as you do, for reasons similar to the reason why you create nested classes.

Microscope answered 22/2, 2013 at 12:3 Comment(5)
a method group conversion only occurs when we have assigned a method thats overloaded, and at least one overload matches the delegate. In this case there is no method group conversion.Scorch
@Scorch I was suggesting he changed his code into this: MyDelegate myDel = AddNumbers; myDel += MultiplyNumbers; If he did that, a method group conversion would happen to instantiate his type MyDelegate. It's a method group conversion even if the "group" of overloads (including generic overloads) consists of just one overload.Microscope
if you provide me a reference to some documentation i will believe you. Until then i will stick to the original ECMA documentation that states my previous comment. There must be a method group with the same name in the same class / struct so we can have a method group conversion.Scorch
@Scorch If you don't think MyDelegate myDel = AddNumbers; (with names referring to the question) is a method group conversion, then what would it be then? Read the C# Language Specification where the relevant section begins: An implicit conversion (§6.1) exists from a method group (§7.1) to a compatible delegate type. Given a delegate type D and an expression E that is classified as a method group, an implicit conversion exists from E to D if [...] Also try saying var x = AddNumbers;. The compiler will respond CS0815: Cannot assign method group to an implicitly-typed local variableMicroscope
Nine years later: With C# 10.0, the statement var x = AddNumbers; is no longer a compile-time error. However, it will be Action<int, int> that will be picked as the type for x, not MainPage.MyDelegate.Microscope
M
2

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.

Mutable answered 2/7, 2015 at 12:32 Comment(1)
Related: Is there a Delegate which isn't a MulticastDelegate in C#?Maisiemaison
A
1

Multicast delegates is one of the feature of delegates, it wraps the reference of multiple methods and calls it sequentially and it is also known as Delegate Chaining.

Below is the example of multicast delegates.

    // Declare Delegates
public delegate void MultiCast(int num1, int num2);

class Program
{
    public void Add(int num1, int num2)
    {
        Console.WriteLine(num1 + num2);
    }
    public  void Sub(int num1, int num2)
    {
        Console.WriteLine(num1 - num2);
    }
    public  void Mul(int num1, int num2)
    {
        Console.WriteLine(num1 * num2);
    }

    static void Main(string[] args)
    {
        MultiCast del1, del2, del3, multAddDel, multSubDel;
        del1 = new Program().Add;
        del2 = new Program().Sub;
        del3 = new Program().Mul;

    //`There are three ways to define the multicast delegate.`

        //1 way

        //Adding delegates 
        multAddDel = del1 + del2 + del3;
        multAddDel(10, 10);
        //Removing Delegates
        multSubDel = multAddDel - del3;
        multSubDel(10, 10);

        Console.WriteLine();
        Console.WriteLine("Second Way");

        //2 way

        MultiCast multAddDel1 = null;
        //Adding delegates 
        multAddDel1 += del1;
        multAddDel1 += del2;
        multAddDel1 +=  del3;
        multAddDel1(10, 10);
        //Removing Delegates
        multAddDel1 -= del3;
        multAddDel1(10, 10);

        Console.WriteLine();
        Console.WriteLine("Third Way");

        //3 way

        MultiCast multAddDel2 = null;
        //Adding delegates 
        multAddDel2 = (MultiCast)Delegate.Combine(multAddDel2, del1);
        multAddDel2 = (MultiCast)Delegate.Combine(multAddDel2, del2);
        multAddDel2 = (MultiCast)Delegate.Combine(multAddDel2, del3);
        multAddDel2(10, 10);

        //Removing Delegates
        multAddDel2 = (MultiCast)
            Delegate.Remove(multAddDel2, del3);

        multAddDel2(10, 10);
        Console.ReadLine();
    }
}
Aikido answered 1/4, 2017 at 18:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.