By 22/07/2015 7 Comments

C# Multicast Delegates

  • In C# Multicast is a powerful feature of delegates.
  • Through the multicast feature we can create a list or chain of multiple methods, that chain or list will be called automatically when its associated delegate is invoked/called.
  • We can create the multicast method chain/list by simply assigning/add methods into delegate reference variable. Use += operator to add methods into the chain and to remove a method from the chain/list use -= operator.
  • Remember the delegate which uses multicast usually has void as its return type, it means that the methods which is added into multicast chain also can be void types.

Example:

using System;
namespace csharpAdvance
{
    // A void type delegate.
    delegate void PLanguageDeleg();

    class PLanguages
    {
        // 4 static void type null parameterized methods are declared as following.
        public static void DisplayAsp()
        {
            Console.WriteLine("This is ASP.NET.");
        }

        public static void DisplayPhp()
        {
            Console.WriteLine("This is Php.");
        }

        public static void DisplayJs()
        {
            Console.WriteLine("This is Javascript.");
        }

        public static void DisplayJava()
        {
            Console.WriteLine("This is Java.");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // Following statement declares delegate reference variable.
            PLanguageDeleg planguageDeleg;

            // Each method is added into a delegate chain through += operator.
            planguageDeleg = PLanguages.DisplayAsp;
            planguageDeleg += PLanguages.DisplayJava;
            planguageDeleg += PLanguages.DisplayJs;
            planguageDeleg += PLanguages.DisplayPhp;

            // Delegate is called or invoked 1st time.
            planguageDeleg();

            // Creating white spaces.
            Console.WriteLine();
            Console.WriteLine();

            // 2 methods are removed from a delegate chain through -= operator.
            planguageDeleg -= PLanguages.DisplayJava;
            planguageDeleg -= PLanguages.DisplayPhp;

            // Delegate is called or invoked 2nd time.
            planguageDeleg();

            Console.ReadKey();
        }
    }
    /*  The Output will be:
     
        This is ASP.NET.
        This is Java.
        This is Javascript.
        This is Php.


        This is ASP.NET.
        This is Javascript.
     */
}





Next Tutorial →


← Previous Tutorial


Posted in: C# Advance, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .