Tuesday, October 11, 2016

What events do which delegates don't

Events v/s Delegates in .Net

If someone is a .Net developer he/she might have heard faced this famous question of what is the difference between events and delegates? Here the differences are summarized and given links to external sites as there are many resources available on the same and don't wanted to create another duplicate.

As everyone knows without delegates where is no event and event is just a multicast delegate. 

Invocation

The main difference is that event cannot be raised/invoked from outside of the class which declare it. If an object exposes a delegate as property or field anybody who has access to that property can invoke that delegate.

Protection

If we expose an delegate as public property, the consumers can overwrite other consumers. But in case of events its not possible. Events just add new subscriber to the existing list of subscribers.

Declaration

 An interface can declare and event and force the implementer to raise notification.This can be done by having a method in interface which accepts and delegate and get that invoked by implementer(s). But declaring event is more readable or easy to use.

No comments: