Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why this multicast Deligation isn’t working with functions in C#?

I’m a newbie in C# and exercising multicast delegation , I made 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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 long post , Thanks in advance

>Solution :

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);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading