Difference between Events and Delegates.

People don’t see the difference between events and delegates sometimes. Delegate is a class that contains a field that holds a reference to a method and then you can call the method with appropriate parameters. Whereas an event is a variable that you can define and specify one or more delegates to be added to it.

Delegates

Delegate is a function pointer. It holds reference to the function. When we instantiate delegate we pass function name to it.

Every delegate has his own signature:

delegate int MyDelegate(string s, bool b);

So, the function that we can pass it must have the same signature:

private int SampleFunction(string str, bool bln){...}

Let’s create delegate:

MyDelegate md = new MyDelegate(SampleFunction);

Now, md refers to SampleFunction. In other words SampleFunction is registered to md. If you call md,SampleFunction will be invoked.

md("sample-string", true);

Events

Events are the actions(from semantic perspective) that raised by an object when certain conditions are met. In ASP.NET Web forms, for example, you have Button class. When you click on it, the Click event fires. Events are objects of type delegates. So, if you want to declare an event, you have to declare a variable of type some delegate and put event keyword before your declaration, in our example with Button, the event looks like this:

public event EventHandler Click;

Here EventHandler is a delegate and it’s signature looks like this:

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

In order to register new function to the Button’s click event

void Page_Load(Object sender, EventArgs e)
{
    Button1.Click += new EventHandler(this.GreetingBtn_Click);
}

void GreetingBtn_Click(Object sender,EventArgs e)
{
    // our logic
}

Notice that += says that delegate can hold references to more than one object. So, if we have two functions attached to it, after raising an event both of them will be invoked one after another.

Also one important thing to notice is that event keyword prevents any of the delegate’s users setting it to null. It allows only to add(+=) or remove(-=) delegates from the invocation list.

So why use Events instead delegates? Rationale of using events instead of delegates is the the same as for using properties instead of fields - data encapsulation. It’s bad practice to expose fields (whatever they are – primitive fields or delegates) directly.

Summarize : an event is defined as a set of accessor methods that encapsulate a delegate in the same way as a property encapsulates a regular field. Where a property defined “get” and “set”, an event defines “add” and “remove”. That is all an event is. This provides the same abstraction as properties, and formalizes the contract: you can subscribe and unsubscribe, but you don’t have direct control of the delegate. So the main difference is that event provides one more level of encapsulation over delegates. When we pass delegates it’s naked and the destination / subscriber can modify the delegate. When we use events the destination can only listen to it.

Share this post:Tweet about this on TwitterShare on Facebook0Share on LinkedIn0Share on Google+0Share on Reddit0Email this to someoneDigg this