How to C# Static vs Instance Methods As Event Handlers

View: 423    Dowload: 0   Comment: 0   Post by: hanhga   Category: C / C++ / MFC   Fields: Other

0 point/0 review File has been tested

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.
      
     */
}

How to C# Static vs Instance Methods As Event Handlers

How to C# Static vs Instance Methods As Event Handlers Posted on 12-09-2016  Both static and instance methods can be used as event handlers in C# 5/10 423

Comment:

To comment you must be logged in members.

Files with category

  • An autonomous and concealed IPC mechanism

    An autonomous and concealed IPC mechanism

    View: 38    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: none

    What if you have one or more modules, running independently in a system, which should collaborate between each other? Well, they will have to be able to exchange data, organized as events, messages or packets of some sort.

  • How to Swap Two Numbers using Call by Reference in C

    How to Swap Two Numbers using Call by Reference in C

    View: 551    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: Other

    How to swap two numbers using call by reference in C , C++. In the interviews interviewer generally asked the difference between call by reference and call by value

  • How to Print Fibonacci Series using Recursion in C, C++

    How to Print Fibonacci Series using Recursion in C, C++

    View: 548    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: Other

    Write a program to print Fibonacci Series using recursion. Fibonacci series is a very important program in terms of interviews. To make this program little tough interviewer ask to write a fibonacci series code using recursion.

  • How to Implicitly Typed Arrays In C#

    How to Implicitly Typed Arrays In C#

    View: 617    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: Other

    When we initialize these type of arrays with any data type, the compiler convert these arrays into that data type at compile time.

  • How to C# Static vs Instance Methods As Event Handlers

    How to C# Static vs Instance Methods As Event Handlers

    View: 423    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: Other

    Both static and instance methods can be used as event handlers in C#

  • Build Pass By Reference To Method In C#

    Build Pass By Reference To Method In C#

    View: 578    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: Other

    Like value types such as int, double, char etc. We can also pass a reference variable to the method in C# as its parameter which allows us to pass an object to the method and it is called pass by reference to method in C#.

  • How to C# Continue Statement

    How to C# Continue Statement

    View: 395    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: Other

    continue statement skips or exits the current execution of a loop by given condition and keep execution continue for its next iteration unlike break statement it does not exit or terminate from loops, it terminates only its current execution

  • How to Single Dimensional Arrays In C#

    How to Single Dimensional Arrays In C#

    View: 344    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: Other

    Single Dimensional Arrays in C# store each individual element at their single dimension, position or a specific location, that location is called an array index. Each index describes the position of each element within an array. Each element in an...

 
File suggestion for you
File top downloads
Codetitle.net - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.net Develope by Vinagon .Ltd