C# Program to Implement Multicast Delegates
Posted by Superadmin on August 14 2022 03:29:40

C# Program to Implement Multicast Delegates

 

This is a C# Program to implement multicast delegates.

Problem Description

This C# Program Implements Multicast Delegates.

Problem Solution

Here Multicast delegate is a delegate which holds a reference to more than one method.

Program/Source Code

Here is source code of the C# Program to Implement Multicast Delegates. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Implement Multicast Delegates
 */
using System;
delegate void dele(int a, int b);
public class Oper
{
    public static void Add(int a, int b)
    {
        Console.WriteLine("{0} + {1} = {2}", a, b, a + b);
    }
 
   public static void Sub(int a, int b)
    {
        Console.WriteLine("{0} - {1} = {2}", a, b, a - b);
    }
}
public class program
{
    static void Main()
    {
        dele del = new dele(Oper.Add);
        del += new dele(Oper.Sub);
        del(4, 2);
        del -= new dele(Oper.Sub);
        del(1, 9);
        Console.Read();
    }
}
Program Explanation

This C# program is used to implement multicast delegates. Using Add and Sub two methods perform addition and subtraction. The Multicast delegate is a delegate which holds a reference to more than one method. Using the delegate object variable ‘del’ we are calling the methods by passing the value as argument.

 
Runtime Test Cases
 
4 + 2 = 6
4 - 2 = 2
1 + 9 = 10