By 22/07/2015 1 Comments

C# Static vs Instance Methods As Event Handlers

  • Both static and instance methods can be used as event handlers in C#.
  • Static and instance methods/handlers are different from each other in one reason: When we use a static method as a handler, an event notification is received by a class.
  • When an instance method is used as a handler, a specific object receives an event notification OR when there are multiple objects of a single class, each object of a class receives an event notification separately.
  • Each object of a class that is interested to receive an event notification must register/add their handler/method to an event individually.
  • In real world examples, most handlers are instance methods.

Example – 1, An instance method as an event handler:

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

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

        // Public void type method is declared.
        public void OnOperations()
        {
            if (ClickTasks != null)
                ClickTasks();
            else
                Console.WriteLine("Please register a handler to an event !");
        }
    }
    class Add
    {
        int Id;

        // 1 parameterized constructor.
        public Add(int id)
        {
            this.Id = id;
        }

        // An instance void method handler.
        public void Addition()
        {
            Console.WriteLine("An event is recieved by Object {0} ", this.Id);
        }
    }

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

            Add obj1 = new Add(1);
            Add obj2 = new Add(2);
            Add obj3 = new Add(3);
            Add obj4 = new Add(4);

            /* The handler/method (Addition) of each object 
               is registered/added to a multicast event chain 
               individually.*/
            calculatorEvent.ClickTasks += obj1.Addition;
            calculatorEvent.ClickTasks += obj2.Addition;
            calculatorEvent.ClickTasks += obj3.Addition;
            calculatorEvent.ClickTasks += obj4.Addition;

            // Fire an event through its object reference.
            calculatorEvent.OnOperations();
            Console.ReadKey();
        }
    }
    /*  The Output will be:
     
        An event is recieved by Object 1
        An event is recieved by Object 2
        An event is recieved by Object 3
        An event is recieved by Object 4
     
     */
}

Example – 2, A static method as an event handler:

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

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

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

    class Add
    {
        // A static void method handler.
        public static void Addition()
        {
            Console.WriteLine("An event is recieved by a class.");
        }
    }

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

            // The handler/method (Addition) is registered/added to an event.
            calculatorEvent.ClickTasks += Add.Addition;

            // Fire an event through its object reference.
            calculatorEvent.OnOperations();
            Console.ReadKey();
        }
    }
    /*  The Output will be:
     
        An event is recieved by a class.
     
     */
}





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 .