I don't see advantages of using events over delegates, other than being syntactical sugar. Perhaps I am misunderstanding, but it seems that event is just a placeholder for delegate.
Would you explain to me the differences and when to use which? What are the advantages and disadvantages? Our code is heavily rooted with events, and I want to get to the bottom of it.
When would you use delegates over events and vice versa? Please state your real world experience with both, say in the production code.
Events are syntactical sugar. They are delicious. When I see an event, I know what to do. When I see a delegate, I'm not so sure.
Combining events with interfaces (more sugar) makes for a mouth watering snack. Delegates and pure virtual abstract classes are much less appetizing.
to understand the differences you can look at this 2 examples
Exemple with Delegates (Action in this case that is a kind of delegate that doen't return value)
to use the delegate you should do something like this
this code works well but you could have some weak spots.
For example if I write this
with the last line of code I had override the previous behaviors just with one missing
+
(I have used+
instead of+=
)Another weak spot is that every class that use your
Animal
class can raiseRaiseEvent
just calling itanimal.RaiseEvent()
.To avoid this weak spots you can use
events
in c#.Your Animal class will change in this way
to call events
Differences:
notes
EventHandler is declared as the following delegate:
it takes a sender (of Object type) and event arguments. The sender is null if it comes from static methods.
You can use also
EventHAndler
instead this example that useEventHandler<ArgsSpecial>
refer here for documentation about EventHandler
The keyword
event
is a scope modifier for multicast delegates. Practical differences between this and just declaring a multicast delegate are as follows:event
in an interface.public event
).As a matter of interest, you can apply
+
and-
to multicast delegates, and this is the basis of the+=
and-=
syntax for the combination assignment of delegates to events. These three snippets are equivalent:Sample two, illustrating both direct assignment and combination assignment.
Sample three: more familiar syntax. You are probably acquainted with the assignment of null to remove all handlers.
Like properties, events have a full syntax that no-one ever uses. This:
...does exactly the same as this:
The add and remove methods are more conspicuous in the rather stilted syntax that VB.NET uses (no operator overloads).
Although I've got no technical reasons for it, I use events in UI style code, in other words, in the higher levels of the code, and use delegates for logic that lays deeper in the code. As I say you could use either, but I find this use pattern to be logically sound, if nothing else, it helps document the types of callbacks and their hierarchy too.
Edit: I think the difference in usage patterns I have would be that, I find it perfectly acceptable to ignore events, they are hooks/stubs, if you need to know about the event, listen to them, if you don't care about the event just ignore it. That's why I use them for UI, kindof Javascript/Browser event style. However when I have a delegate, I expect REALLY expect someone to handle the delegate's task, and throw an exception if not handled.