Events in C#

The Event is something special that is going to happen. Here we will take an example of an event, where Microsoft launches the events for the developer. In this Event, Microsoft wants to aware the developer about the feature of the existing or new products. For this, Microsoft will use Email or other advertisement options to aware the developer about the Event. So, in this case, Microsoft will work as a publisher who raises the Event and notifies the developers about it. Developers will work as the subscriber of the Event who handles the Event.

Similarly, in C#, Events follow the same concept. In C#, Event can be subscriber, publisher, subscriber, notification, and a handler. Generally, the User Interface uses the events. Here we will take an example of Button control in Windows. Button performs multiple events such as click, mouseover, etc. The custom class contains the Event through which we will notify the other subscriber class about the other things which is going to happen. So, in this case, we will define the Event and inform the other classes about the Event, which contains the event handler.

The event is an encapsulated delegate. C# and .NET both support the events with the delegates. When the state of the application changes, events and delegates give the notification to the client application. Delegates and Events both are tightly coupled for dispatching the events, and event handling require the implementation of the delegates. The sending event class is known as the publisher, and the receiver class or handling the Event is known as a subscriber.

Points to remember

The key points about the events are as:

  1. In C#, event handler will take the two parameters as input and return the void.
  2. The first parameter of the Event is also known as the source, which will publish the object.
  3. The publisher will decide when we have to raise the Event, and the subscriber will determine what response we have to give.
  4. Event can contain many subscribers.
  5. Generally, we used the Event for the single user action like clicking on the button.
  6. If the Event includes the multiple subscribers, then synchronously event handler invoked.

Declaration of the Event

Syntax
public event EventHandler CellEvent;

Steps for implementing the Event

For the declaration of the Event in the class, firstly, the event type of the delegate must be declared.

public delegate void CellEventHandler(object sender, EventArgs e);

Declaration of the Event

public event CellEventHandler CellEvent;

Invokation of the Event

if (CellEvent != null) CellEvent(this, e);

We can invoke the Event only from within the class where we declared the Event.

Hooking up the Event

OurEventClass.OurEvent += new ChangedEventHandler(OurEventChanged);

Detach the Event

OurEventClass.OurEvent -= new ChangedEventHandler(OurEventChanged);

Delegates

Delegates work as pointer to a function. It is a reference data type and it holds the reference of the method. System.Delegate class implicitly derived all the delegates.

Delegate can be declared using the delegate keyword which is followed by the signature

Syntax
snippet
<access modifier> delegate <return type> <delegate_name>(<parameters>)
Example

public delegate void PrintWord(int value);

The above PrintWord delegate can be used to point any method which has the same return type and declared parameters with PrintWord. Here we will take an example that declares and uses the PrintWord delegates.

snippet
class Program1
{
    // declare delegate
    public delegate void PrintWord(int value);

    static void Main(string[] args)
    {
        // Print delegate points to PrintNum
        PrintWord printDel = PrintNum;
        
        // or
        // Print printDel = new Print(PrintNumber);
            
        printDel(100000);
        printDel(200);

        // Print delegate points to PrintMoney
        printDel = PrintMoney;

        printDel(10000);
        printDel(200);
    }

    public static void PrintNum(int num)
    {
        Console.WriteLine("Number: {0,-12:N0}",num);
    }

    public static void PrintMoney(int money)
    {
        Console.WriteLine("Money: {0:C}", money);
    }
}
Events in C#

In the above example, we declared the PrintWord delegates, which accepts the int type parameter and returns the void. In the main() method, we declare the PrintWord type method and assigned the PrintNum name method. Now we will invoke the PrintWord delegate, which in-turn invokes the PrintNum method. In the same way, if the PrintWord delegates variable is assigned to the PrintMoney method, then this will invoke the PrintMoney method.

Also, we can create the delegate object by using the new operator and specify the name of the method, as shown below:

snippet
PrintWord printDel = new PrintWord(PrintNum);

Delegates can be declared, as shown below:

snippet
public delegate void someEvent();
public organize event
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +