In case you are familiar with the syntax surrounding delegates and events in c# you might have wondered why you need to use the event keyword. The reason is encapsulation.
In below discussion delegates are the objects that were defined without the event keyword.
Subscriber encapsulation
Delegates allow syntax of the form :
someDelgate = someMethod;
This will allow a rouge subscriber to remove all previous subscribers.
Events allow assignment only in the encapsulating class.
Publisher encapsulation
Delegates allow syntax of the form :
someObject.SomeDelegate();
That is anyone can invoke public delegate members. Events only allow the encapsulating class to invoke the event.
Additionally
You can initialize events so that you do not need to do null checking before invocation using the "= delegate {}" syntax.
PS: this was written on my iPad. Definitely pulling out my laptop in future.
It's not bad...just weird.
No comments:
Post a Comment