#1,159 – Events vs. Delegates, part II

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.

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

2 Responses to #1,159 – Events vs. Delegates, part II

  1. Pingback: Dew Drop – August 14, 2014 (#1836) | Morning Dew

  2. Pingback: How to mimic singlecast events in C# - Rico Suter's site Rico Suter's site

Leave a comment