Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
What is the difference between Event and Delegates. In lots of places we have to faced this question. But not from every sites we got right answer.
Posted

What's wrong with standard MSDN overview: http://msdn.microsoft.com/en-us/library/17sde2xt.aspx[^]?

Many experts would provide good answers, but I'm not sure it would justify the time spend. Start reading about these concepts and do simple exercises to get a feel of it and remove misunderstanding. When something is not clear, ask some more concrete questions — I would be happy to help, or someone else will. If you know all what can be considered as a prerequisite to these topics, it could be understood pretty fast, but I cannot say it is trivial — not at all.

[EDIT]

Please also see my comment to the answer by Ravi.

Finally, I would like to add that understanding of delegates, delegate instances (notice the difference) and events need significant effort, but after basic usage pattens are learned the usage is very easy and safe.

—SA
 
Share this answer
 
v3
Comments
Michel [mjbohn] 21-Apr-11 1:29am    
my 5
Sergey Alexandrovich Kryukov 21-Apr-11 1:30am    
Thank you very much.
--SA
Ankur\m/ 21-Apr-11 1:46am    
Many experts would provide good answers, but I'm not sure it would justify the time spend.
I can't agree more. The topic is already very well explained (MSDN and other articles).

good advice, my 5!
Sergey Alexandrovich Kryukov 21-Apr-11 2:28am    
Thank you, Ankur.
(I you don't mind, I fixed a typo (copied from mine, fixed, too, sorry) and one usage mistake...)
--SA
Kim Togo 21-Apr-11 1:53am    
My 5.
SA already given you a perfect solution In addition to his answer my answer is
Delegates and Events Both are related.
Dalegete is a function pointer which can able to store the address of any function with same prototype where as Event is a function handler which can handles or run the functions in same prototype of its delegate.So for handling events delegate is used.
Take a look there[^] to read a Nice article which focusing on Delegate and Events. Its not too much you can get plenty of article and Solutions over Google which will easily guide you what's the difference.
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 21-Apr-11 1:37am    
Sorry, Ravi, this is not accurate. If you examine a delegate instance, you will see it is a... class, and it contains an invocation list with one or more "function pointer". And even each of the pointers is not like a function pointer, it also contains "this" (null for static handlers), otherwise how a non-static method could be called? Not so simple.

The major difference between event and a delegate is event limitations and the binding of to generic EventHander. This topic also would need a short article. "So for handling events delegate is used" is not accurate. As someone noticed, the relationship between event and delegate instance resembles the one between property and field (setter/getter), you can program side effect of += and -+.

--SA
GlobX 21-Apr-11 1:44am    
SA, beautiful response - it's not often you find someone with the ability to tell someone they're wrong without being harsh or rude :)
Sergey Alexandrovich Kryukov 21-Apr-11 2:43am    
Thank you for your good words.
Well, very many people say I am harsh -- I have a different culture where your best friend will never miss a chance to hit your face in dirt (in terms of understanding science, if you try to pretend you're an "expert"). Such relationships is a good school which teaches positive acceptance of criticism and self-improvement. People respect each other when the challenge like that but will not respect anyone getting criticism as offense. Here in US I often see people who would get it as an offense if you simply say "You solution will not work", not everyone, but... How to do engineering in this case?
--SA
RaviRanjanKr 21-Apr-11 3:08am    
hey SA! I am a student of Computer Science and I always looking for expert advice. I respect to my elders and seniors like you.
CP gives me a great platform for learning and sharing knowledge.
and I will keep looking to learn a lots of thing from you and other experts of CP. :)
Sergey Alexandrovich Kryukov 21-Apr-11 3:37am    
I really respect your active and interested learning; it looks like you already doing good; It's my pleasure to work with such people!

(I hope you understand that we discussed harshness and attitude to criticism in different people with GlobX, in contrast with you; for example, see the reference to a comment to my other answer below).

Respect,
--SA
 
Share this answer
 
Comments
Prasanta_Prince 21-Apr-11 5:02am    
Good solution. Thank you.
RaviRanjanKr 21-Apr-11 8:15am    
Thank you Prasanta :)
Hope this[^] might help you.
 
Share this answer
 
As Appendix to SA. I find this CP article very help full C# Event Implementation Fundamentals, Best Practices and Conventions[^]. I have learned a lot from this articel about events and delegates. At first in C# I self was confused about the differences and how it works.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900