C# program to demonstrate the example of multicast delegate

I hope every article can give you some new information. In that sense today we are going to know very clearly about C# program to demonstrate the example of multicast delegate. Let’s go into our article.

C# program to demonstrate the example of multicast delegate

C# program to demonstrate the example of multicast delegate

//C# program to demonstrate multicast delegate.

using System;

delegate void MyDel(int num1,int num2);
class Sample
{ 
    static void Add(int num1, int num2)
    {
        Console.WriteLine("\tAddition: "+(num1+num2));
    }

    static void Sub(int num1, int num2)
    {
        Console.WriteLine("\tSubtraction: " + (num1 - num2));
    }

    static void Main()
    {
        int num1   = 0;
        int num2   = 0;

        MyDel del = new MyDel(Add);

        Console.Write("Enter the value of num1: ");
        num1 = int.Parse(Console.ReadLine());

        Console.Write("Enter the value of num2: ");
        num2 = int.Parse(Console.ReadLine());

        del += new MyDel(Sub);

        Console.WriteLine("Call 1:");
        del(num1, num2);
        
        del -= new MyDel(Sub);
        Console.WriteLine("Call 2:");
        del(num1, num2);
        
    }
}
Enter the value of num1: 10
Enter the value of num2: 5
Call 1:
        Addition: 15
        Subtraction: 5
Call 2:
        Addition: 15
Press any key to continue . . .

Final Words

C# program to demonstrate the example of multicast delegate Article We hope you have all the information you need. Let me know if you have any doubts about this article. Thanks.

Hi, I'm Selva a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Deekshi. Here, I post about programming to help developers.

Share on:

Leave a Comment