#1,159 – Events vs. Delegates, part II
August 13, 2014 2 Comments
You can use a delegate in place of an event by simply removing the event keyword. Using an event is preferred, however, because the event syntax prevents different consumers of the event from impacting each other.
Specifically, using a public field that is a delegate allows client code to do any of the following. These actions are all prohibited when using an event.
- Prevent other subscribers from being invoked by directly changing the delegate’s invocation list.
- Delete invocation list by reassigning the delegate instance to null
- Invoke the delegate directly
These are all actions that should only be allowed by the producer of the event and not by any of its consumers. The event syntax prevents consumers from interfering with each other.