How to implement Event Accessors and Single Handler Events

Event accessor is not new concept. It has been part of C# language from its inception. If you have ever built a class which has an Event with it, you might have already used it. The .NET automatically adds one Event Accessor inside the event when you define one without it. Unlike VB.NET, the event accessor does not have a specific node which defines the RaiseEvent block, so in case of C#, we need a separate method to raise an event. Let us see how to do that.

private event EventHandler myEvent;

public event EventHandler MyEvent
{
    add 
    {
       this.myEvent += value;
    }
    remove 
    {
       this.myEvent -= value;
    }
}

void OnMyEvent()
{
    if(this.myEvent != null)
       this.myEvent(this, EventArgs.Empty);
}

Here in the code above, you can see, we have created an event as private and added an Event Accessor. When a Handler is added to the event, it will call the Add method of the Event handler while remove is called when an event handler is removed.

When the application wants to raise an event, it need to call the OnMyEvent to call the event delegate. It is important to check whether the event is null or not for every call to the event because events are meant to be used as subscription, and if the user didn’t added a handler, the call to the event could produce a NullReferenceException.

As you know, Events in .NET is a wrapper to a multicast delegate, you can add as many event handler as you want. But there could be a situation where you need SingleTon event. Here is an example.

How to create Single Handler Events

In case of Single Handler events, there could be only one event handler for a particular event. So when an event is called again, it will nullify the previous event and add the new eventhandler again.


private event EventHandler myEvent;
private EventHandler MyEventSingleHandler {get;set;}
public event EventHandler MyEvent
{
    add 
    {
      if(MyEventSingleHandler != null)
         this.myEvent -= this.MyEventSingleHandler;
       this.myEvent += value;
       this.MyEventSingleHandler = value;
    }
    remove 
    {
       this.myEvent -= value;
       this.MyEventSingleHandler = null;
    }
}

void OnMyEvent()
{
    if(this.myEvent != null)
       this.myEvent(this, EventArgs.Empty);
}

Here we keep track of the EventHandler inside the object separately, such that when a new eventhandler is added to the object, it will first remove the previous one and then add the new one. The object hence created will ensure it will only have a single eventhandler inside an object.

I hope the post will come handy.

Happy programming

Abhishek Sur

Abhishek Sur is a Microsoft MVP since year 2011. He is an architect in the .NET platform. He has profound theoretical insight and years of hands on experience in different .NET products and languages. He leads the Microsoft User Group in Kolkata named KolkataGeeks, and regularly organizes events and seminars in various places for spreading .NET awareness. He is associated with the Microsoft Insider list on WPF and C#, and is in constant touch with product group teams. He blogs at http://www.abhisheksur.com His Book : Visual Studio 2012 and .NET 4.5 Expert Development Cookbook. Follow Abhishek at Twitter : @abhi2434

One Comment to “How to implement Event Accessors and Single Handler Events”

  1. Pingback: hacer

Comments are closed.