Home > Back-end >  Why this multicast delegation isn’t working with functions in C#?
Why this multicast delegation isn’t working with functions in C#?

Time:03-20

I'm a newbie in C# and exercising multicast delegation, I wrote this code:

class MyClass
{
    public static double Calculate(int x, double z)
    {
        double Result = (x / z);
        return Result;
    }

    public static double Calculate2(int x, double z)
    {
        double Result = (x * z);
        return Result;
    }
}

public class Program
{
    public delegate double MyDlgt(int Number1, double Number2);

    public static void Main()
    {
        Console.WriteLine("What is You First Value ? ");
        int NUM1 = int.Parse(Console.ReadLine());
        Console.WriteLine("What is You Second Value ? ");
        Double NUM2 = Double.Parse(Console.ReadLine());

        MyDlgt FirstDlgt = new MyDlgt(MyClass.Calculate);
        MyDlgt SecondDlgt = new MyDlgt(MyClass.Calculate2);

        Console.WriteLine("<---------Simple Delegate --------->");

        Console.WriteLine("The Division Result is : "   FirstDlgt(NUM1, NUM2));
        Console.WriteLine("The Multiply Result is : "   SecondDlgt(NUM1, NUM2));

        Console.WriteLine("");
        Console.WriteLine("<---------MultiCast Delegate --------->");
        MyDlgt MultiCast = FirstDlgt   SecondDlgt;
        Console.WriteLine("The Result is : "   MultiCast(NUM1, NUM2));
        Console.Read();
    }
}

The multicast is only showing the second function I don't know why.

The result is

What is You First Value ? 
10
What is You Second Value ? 
20
<---------Simple Delegate --------->
The Division Result is : 0.5
The Multiply Result is : 200

<---------MultiCast Delegate --------->
The Result is : 200

I tried multicast with void and working perfectly

class MyClass
{
        public static void Calculate(int x, double z)
        {
            Console.WriteLine("The Result is :"  (x / z));
        }

        public static void Calculate2(int x, double z)
        {
            Console.WriteLine("The Result is :"   (x * z));
        }

        public static void Main()
        {
            Console.WriteLine("What is You First Value ? ");
            int NUM1 = int.Parse(Console.ReadLine());
            Console.WriteLine("What is You Second Value ? ");
            Double NUM2 = Double.Parse(Console.ReadLine());

            MyDlgt FirstDlgt = new MyDlgt(MyClass.Calculate);
            MyDlgt SecondDlgt = new MyDlgt(MyClass.Calculate2);
           
            Console.WriteLine("<---------Simple Delegate --------->");
           
            FirstDlgt.Invoke(NUM1, NUM2);
            SecondDlgt.Invoke(NUM1, NUM2);
            
            Console.WriteLine("");
            Console.WriteLine("<---------MultiCast Delegate --------->");
            MyDlgt MultiCast = FirstDlgt   SecondDlgt;
                     
            MultiCast(NUM1, NUM2);
            Console.ReadLine();
        }
    }
}

The result is correct :

What is You First Value ? 
10
What is You Second Value ? 
20
<---------Simple Delegate --------->
The Result is :0.5
The Result is :200

<---------MultiCast Delegate --------->
The Result is :0.5
The Result is :200

Why it is working with void and not with functions with return ?

Sorry for the long post, thanks in advance

CodePudding user response:

According to the spec:

If the delegate invocation includes output parameters or a return value, their final value will come from the invocation of the last delegate in the list.

If you need more control over individual results, then use GetInvocationList():

foreach (MyDlgt myDlgt in MultiCast.GetInvocationList())
{
    double result = myDlgt(NUM1, NUM2);
}
  • Related