Home > DeveloperSection > Blogs > Multicast Delegate in C#

Multicast Delegate in C#


C# C# 
Ratings:
0 Comment(s)
 5164  View(s)
Rate this:

Multicast Delegates

Such type’s delegates which hold and invoke multiple methods are known as multicast delegates. But simple delegates invoke only one method. Multicast delegates must satisfy the following condition:

·         The return type of the delegate must void.

·         None of the parameters of the delegate type can be declared as output parameters, using out keywords.

For Example:

delegate void Multidelegate();

    class Program

    {

        public static void Main()

        {

            Multidelegate mshow1 = new Multidelegate(Class1.show1);

            Multidelegate mshow2 = new Multidelegate(Class1.show2);

            Multidelegate mshow3 = mshow1 - mshow2;

            Multidelegate mshow4 = mshow2 - mshow1;

            Multidelegate mshow5 = mshow3 - mshow2;

            Multidelegate mshow6= mshow5 - mshow4;

            mshow3();

            mshow4();

            mshow5();

            mshow6();

            Console.ReadLine();

        }

    }   

    class Class1

    {

        static public void show1()

        {

            Console.WriteLine("India");

        }

        static public void show2()

        {

            Console.WriteLine("England");

        }

    }

Output:

India

England

India

India


Don't want to miss updates? Please click the below button!

Follow MindStick