What is the difference between a delegate and an event?

Hi

A few days back one of my colleague got confused with the difference between a delegate and event. We can do the same thing with both a delegate and an event. There only seems to be a syntactical difference between the 2. So he searched and found the answer to the fact.

Here is the difference between a delegate and an event.

Delegate:

public class VikramDel

{

public delegate void VikramExampleDelegate(int num1,string str1);

public VikramExampleDelegate VikramDeleageteCallback;

}

Event:

public class VikramEvent

{

public delegate void VikramExampleEvent(int num1,string str2);

public event VikramExampleEvent VikramEventCallback;

}

So syntax wise there is only one difference that we have to use the event keyword with the event.

So the question comes why do we have a keyword when the same work can be done without using it. But there is a reason for the existence of the keyword event. Lets take an example how would a client work with this class

VikramDel V = new VikramDel();

V.VikramDeleageteCallback +=new

VikramDel.VikramExampleDelegate (this.VikDelegate);

Here we are adding a new target to the invocation list of the delegate. The same code will work with the other class also without any problem

VikramEvent V = new VikramEvent();

V.VikramEventCallback + =new

VikramEvent.VikramExampleEvent(this.VikDelegate);

But consider a case where by instead of adding a new target to the invocation list of the delegate if I simply set a delegate to a new delegate (The difference is with the + sign being not there).

VikramDel V = new VikramDel();

V.VikramDeleageteCallback =new

VikramDel.VikramExampleDelegate (this.VikDelegate);

This code will work fine here but the same will not work with an event.

So what it means is that if we use the event keyword no client class can set it to null. This is very important. Multiple clients can use the same delegate. After multiple client have added a function to listen to the callback of the delegate. But now one of the client sets the delegate to null or uses the = sign to add a new call back. This means that the previous invocation list will not be used any more. Hence all the previous client will not get any of the callback even if they have registered for the call back.

Hence we can say that the even keyword adds a layer of protection on the instance of the delegate. The protection prevents any client to reset the delegate invocation list. They can only add or remove the target from the invocation list.

Thanks
Vikram


Share this post   Email it |  digg it! |  reddit! |  bookmark it!

Feedback

Posted on 6/12/2007 11:27:38 PM

Thanks for this posting .. really it help me

Posted on 6/13/2007 8:07:28 AM

Hi Vikram

Really great
I usually visIt ur site
U are doing a great job

But I hav one suggestion please dont use ur name in the programming elements.Ya its right that u will get good control over the code but which will make others less understood

Once again I appreciate ur effort

Dileep Varma K
Cochin

Posted on 6/13/2007 8:40:24 AM

HI Dileep,

I will keep the note of your suggestion. on of the reason for putting my name in the code was that I was trying to experiment and see if that can increase some SEO for search through my name

Posted on 6/15/2007 10:37:01 AM

hi vikram
you have done a excellent job. Although i knew delegates and events and the difference between them, your explanation is too gud. keep posting such stuff man !

Posted on 7/27/2007 7:47:07 AM

I appreciate this posting. I was looking for event examples and IPostBackDataHandler info, but this was very good to know as I'm trying to increase my kb for controls to expert. Thanks again, keep on keepin' on!
Dan

Posted on 9/18/2007 6:10:52 PM

Vikram ,

Thats very useful information you have provided.

Can you tell me difference between Static class and singlten implmentation also ?

Posted on 9/18/2007 9:13:56 PM

Hi Vikash,

I have plan for writing a post on single ton design pattern. After that I will also talk about there difference with Static Class.

Posted on 11/19/2007 11:10:36 PM

Hi vikram

Could ou please explain in detail about it

Posted on 4/17/2008 6:41:49 PM

thanks! sounds very nice!

Posted on 8/1/2008 12:43:20 PM

Hi Vikram,
Very good explantion.. Thank you very much.

-Kiru

Posted on 9/16/2008 8:30:01 AM

Great explanation... Thanks

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 
Copyright © 2006 - 2008 Vikram Lakhotia