By 22/07/2015 9 Comments

Multicast Events In C#

  • Like delegates, the multicast events can be created in C#.
  • Multicast events allow multiple objects to respond an event notification.
  • When a multicast event is occurred the multiple handlers are called automatically.
  • Handlers are instance or static methods which might be declared in different classes.
  • In multicast events the handlers (methods) are registered/added to event chain through += operator and unregistered/removed from event chain using -= operator.

Example:

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

    class CalculatorEvent
    {
        // Public event ClickTasks of delegate type is declared.
        public event ClickHandel ClickTasks;

        // public void type method which calls an event.
        public void OnOperations()
        {
            if (ClickTasks != null)
                ClickTasks();
            else
                Console.WriteLine("Please register a handler to an event !");
        }
    }

    // Following is different class declaration with methods.
    class Add
    {
        public void Addition()
        {
            Console.WriteLine("Addition answer: {0}", (10 + 2));
        }
    }

    class Substract
    {
        public void Substraction()
        {
            Console.WriteLine("Substraction answer: {0}", (10 - 2));
        }
    }

    class Multiply
    {
        public void Multiplication()
        {
            Console.WriteLine("Multiplication answer: {0}", (10 * 2));
        }
    }

    class Divided
    {
        public static void Division()
        {
            Console.WriteLine("Division answer: {0}", (10 / 2));
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            CalculatorEvent calculatorEvent = new CalculatorEvent();

            Add add = new Add();
            Substract substract = new Substract();
            Multiply multiply = new Multiply();

            /* Following is a multicast event chain. 
               Through += operator each handler (method) 
               is registered/added to an event chain.*/
            calculatorEvent.ClickTasks += add.Addition;
            calculatorEvent.ClickTasks += substract.Substraction;
            calculatorEvent.ClickTasks += multiply.Multiplication;
            calculatorEvent.ClickTasks += Divided.Division;

            // An event is triggered by calling OnOperations() method.
            calculatorEvent.OnOperations();

            Console.WriteLine();

            /* Following is an event. 
               Through -= operator a handler (method) 
               is unregistered/removed from an event chain. */
            calculatorEvent.ClickTasks -= Divided.Division;

            // Call an event again through object reference.
            calculatorEvent.OnOperations();
            Console.ReadKey();
        }
    }
    /*  The Output will be:
     
        Addition answer: 12
        Substraction answer: 8
        Multiplication answer: 20
        Division answer: 5

        Addition answer: 12
        Substraction answer: 8
        Multiplication answer: 20
     */
}





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 .