The Role Of Events And Delegates in .NET

Events and Delegates play an important role in the .NET framework and they may seem difficult or confusing if you’re just getting started with programming. I’ve seen a lot  of developers struggle with these concepts and I struggled with it myself. All I knew was just to double click a button to give me a button click event. Nothing more nothing less, but if you get a good thorough understanding of these concept, you’ll get to appreciate it more than you already have. In this post I’ll briefly talk about the roles of  events and delegates in .NET.

Event is simply a notification. It’s a way for an object (event raiser) to tell or notify other objects (event listeners/subscribers) of an action (event). Events in .Net provides a way to trigger notifications from an object referred to as the event raiser, to a subscriber (an object that is notified when the event occurs). Events should be familiar to you if you’ve done desktop programming in windows forms or wpf, or if you write web application with web forms. The controls we place in these kinds of application represent a class, and these classes each defines a set of events which are triggered when the user does something to the control, e.g click of a button. However, events are not used only in GUI applications like I mentioned. It could be used as a means to communicate with other part of an application, e.g notifying a customer that there account has been credited or performing some specific action when a customer places an order. Without events there will be no way we can get notified of an action.

However, events will be useless without the existence of delegates in .NET because events are declared using delegates. A delegate is similar to a function pointer in C/C++ because it encapsulates a method such that the method can be called anonymously. It simply means that a delegate holds reference to one or more methods, such that when it is called, it executes all the methods attached to it. On this basis, when an event is declared using a delegate, and that event is raised, it calls and executes all the methods attached to it (in this case, this methods are referred to as event handlers).

The declaration of a delegate type is similar to a method signature such that it has a return value and parameters. It can be used to encapsulate a named or an anonymous method. The delegate must be instantiated with a method or lambda expression that has a compatible return type and input parameters. It is declared using the delegate keyword in C# and what it does behind the scene is create a type that inherit from MulticastDelegate.

When it comes to events, the role of delegates is to create a form of connection between the event itself and the event handler. For example, when a package arrives at a delivery shop or a post office, a delivery man picks up that package and delivers it to the owner and the owner gets the package and does what he/she wants with it. In this example, the event here is the arrival of the package, the delivery shop or the person that monitor the arrival of goods (e.g store keeper) is the event publisher (a class that raises or sends the event), the delivery man can be seen as the delegate, the customer is the event subscriber/handler, and the package the EventArgs. So, when the goods arrives, the store keeper raises the event or rather notify the delivery man who in turn deliver’s the package to the customer.

Knowing that an event can be used to notify users of actions or activities performed in the system, without delegates it would be impossible to achieve, the next step will be creating and handling our own custom event, and that will be left for another day.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s