find and solve || findandsolve.com
Please wait.....

Multicast Delegates in C#

In the C#, Multicast Delegate is a delegate that holds the references of more than one function. When you invoke the multicast delegate, then all the functions. 

What is Multicast Delegate in C#?

A Multicast Delegate in C# is a delegate that holds the references of more than one function. When we invoke the multicast delegate, then all the functions which are referenced by the delegate are going to be invoked. If you want to call multiple methods using a delegate then all the method signatures should be the same.


Multicast Delegate in C# Example

using System;  

using System.Collections.Generic;
using System.Linq;
using System.Text;
delegate void MultipleDelegate();
class DM
{
    static public void Display()
    {
        Console.WriteLine("Test1");
    }
    static public void print()
    {
        Console.WriteLine("Test2");
    }
}
public class MultipleExample
{
    public static void Main()
    {
        MultipleDelegate m1 = new MultipleDelegate(DM.Display);
        MultipleDelegate m2 = new MultipleDelegate(DM.print);
        MultipleDelegate m3 = m1 + m2;
        MultipleDelegate m4 = m2 + m1;
        MultipleDelegate m5 = m3 - m2;
        m3();
        m4();
        m5();
    }
}  

Output

Test1

Test2

Test2

Test1

Test1