What are the differences between delegates and events?Ask Questions

 Posted on 19 days ago

What are the differences between delegates and an events? Don't both hold references to functions that can be executed?

Share On: facebook gplus twitter
profile
Asked by Grayson Omans on 19 days ago Score: 59 points
Add Comment:

Comments

4 Answers

0 Corrected Answers
Aproved Answers
0
Profile
Answered by Praful Chauhan on 10/2/2019 8:16:25 AM Score: 116 points

Delegate is a type-safe function pointer. Event is an implementation of publisher-subscriber design pattern using delegate.

Comments

Add Comment:
Aproved Answers
1
Profile
Answered by Steve Summit on 10/2/2019 8:15:06 AM Score: 35 points

An event in .net is a designated combination of an Add method and a Remove method, both of which expect some particular type of delegate. Both C# and vb.net can auto-generate code for the add and remove methods which will define a delegate to hold the event subscriptions, and add/remove the passed in delegagte to/from that subscription delegate. VB.net will also auto-generate code (with the RaiseEvent statement) to invoke the subscription list if and only if it is non-empty; for some reason, C# doesn't generate the latter.

Note that while it is common to manage event subscriptions using a multicast delegate, that is not the only means of doing so. From a public perspective, a would-be event subscriber needs to know how to let an object know it wants to receive events, but it does not need to know what mechanism the publisher will use to raise the events. Note also that while whoever defined the event data structure in .net apparently thought there should be a public means of raising them, neither C# nor vb.net makes use of that feature.

Comments

Add Comment:
Aproved Answers
0
Profile
Answered by Joseph Sible on 10/2/2019 8:14:28 AM Score: 25 points

In addition to the syntactic and operational properties, there's also a semantical difference.

Delegates are, conceptually, function templates; that is, they express a contract a function must adhere to in order to be considered of the "type" of the delegate.

Events represent ... well, events. They are intended to alert someone when something happens and yes, they adhere to a delegate definition but they're not the same thing.

Even if they were exactly the same thing (syntactically and in the IL code) there will still remain the semantical difference. In general I prefer to have two different names for two different concepts, even if they are implemented in the same way (which doesn't mean I like to have the same code twice).

Comments

I don't get your point.You can use a delegate to 'alert someone when something happens'. Maybe you would not do that, but you can and therefore it's not an inherent property of event.
02/10/2019   08:19:41   Praful Chauhan
So could we say that an event is a "special" type of a delegate?
02/10/2019   08:18:33   Steve Summit
Excellent description of Delegates.
02/10/2019   08:17:50   Praful Chauhan
Add Comment:
Aproved Answers
1
Profile
Answered by Ned Batchelder on 10/2/2019 8:13:14 AM Score: 35 points

An Event declaration adds a layer of abstraction and protection on the delegate instance. This protection prevents clients of the delegate from resetting the delegate and its invocation list and only allows adding or removing targets from the invocation list.

Comments

Add Comment:

Post Your Answers

Existing Members

Sign in to your account
Email Address
Password
New Member?
Sign up and complete profile
Full Name
Email Address
I have read and agree to the Terms of Service and Privacy Policy
Please subscribe me to the StoodQ newsletters
Guideline to answer a question:

Useful tips to submit your answer
Please read below guidelines before you submit your answer for question.

  • Read and understand question for which you are submitting your answer.
  • Try to avoid grammatical and spell mistake while answering.
  • Do not post any irrelevant information in your answer.
  • Explain your answer with example or any reference link to help who posted question.
  • If you find irrelevant question, please report it to support. Click here to contact support.
  • You agree to the privacy policy and terms of use to submit any contents.

Note: StoodQ is online developers community which helps developer for their difficulty, lets help them with your value contribution.