title: C# Events and Thread Safety
body: <p>I frequently hear/read the following advice:</p>
  <p>Always make a copy of an event before you check it for <code>null</code> and fire it. This will eliminate a potential problem with threading where the event becomes <code>null</code> at the location right between where you check for null and where you fire the event:</p>
  <pre><code>// Copy the event delegate before checking/calling
  EventHandler copy = TheEvent;
  
  if (copy != null)
      copy(this, EventArgs.Empty); // Call any handlers on the copied list
  </code></pre>
  <p><em>Updated</em>: I thought from reading about optimizations that this might also require the event member to be volatile, but Jon Skeet states in his answer that the CLR doesn't optimize away the copy.</p>
  <p>But meanwhile, in order for this issue to even occur, another thread must have done something like this:</p>
  <pre><code>// Better delist from event - don't want our handler called from now on:
  otherObject.TheEvent -= OnTheEvent;
  // Good, now we can be certain that OnTheEvent will not run...
  </code></pre>
  <p>The actual sequence might be this mixture:</p>
  <pre><code>// Copy the event delegate before checking/calling
  EventHandler copy = TheEvent;
  
  // Better delist from event - don't want our handler called from now on:
  otherObject.TheEvent -= OnTheEvent;    
  // Good, now we can be certain that OnTheEvent will not run...
  
  if (copy != null)
      copy(this, EventArgs.Empty); // Call any handlers on the copied list
  </code></pre>
  <p>The point being that <code>OnTheEvent</code> runs after the author has unsubscribed, and yet they just unsubscribed specifically to avoid that happening. Surely what is really needed is a custom event implementation with appropriate synchronisation in the <code>add</code> and <code>remove</code> accessors. And in addition there is the problem of possible deadlocks if a lock is held while an event is fired.</p>
  <p>So is this <a href="http://en.wikipedia.org/wiki/Cargo_cult_programming" rel="nofollow noreferrer">Cargo Cult Programming</a>? It seems that way - a lot of people must be taking this step to protect their code from multiple threads, when in reality it seems to me that events require much more care than this before they can be used as part of a multi-threaded design. Consequently, people who are not taking that additional care might as well ignore this advice - it simply isn't an issue for single-threaded programs, and in fact, given the absence of <code>volatile</code> in most online example code, the advice may be having no effect at all.</p>
  <p>(And isn't it a lot simpler to just assign the empty <code>delegate { }</code> on the member declaration so that you never need to check for <code>null</code> in the first place?)</p>
  <p><em>Updated:</em> In case it wasn't clear, I did grasp the intention of the advice - to avoid a null reference exception under all circumstances. My point is that this particular null reference exception can only occur if another thread is delisting from the event, and the only reason for doing that is to ensure that no further calls will be received via that event, which clearly is NOT achieved by this technique. You'd be concealing a race condition - it would be better to reveal it! That null exception helps to detect an abuse of your component. If you want your component to be protected from abuse, you could follow the example of WPF - store the thread ID in your constructor and then throw an exception if another thread tries to interact directly with your component. Or else implement a truly thread-safe component (not an easy task).</p>
  <p>So I contend that merely doing this copy/check idiom is cargo cult programming, adding mess and noise to your code. To actually protect against other threads requires a lot more work.</p>
  <p><strong>Update in response to Eric Lippert's blog posts:</strong></p>
  <p>So there's a major thing I'd missed about event handlers: &quot;event handlers are required to be robust in the face of being called even after the event has been unsubscribed&quot;, and obviously therefore we only need to care about the possibility of the event delegate being <code>null</code>. <em>Is that requirement on event handlers documented anywhere?</em></p>
  <p>And so: &quot;There are other ways to solve this problem; for example, initializing the handler to have an empty action that is never removed. But doing a null check is the standard pattern.&quot;</p>
  <p>So the one remaining fragment of my question is, <em>why is explicit-null-check the &quot;standard pattern&quot;?</em> The alternative, assigning the empty delegate, requires only <code>= delegate {}</code> to be added to the event declaration, and this eliminates those little piles of stinky ceremony from every place where the event is raised. It would be easy to make sure that the empty delegate is cheap to instantiate. Or am I still missing something?</p>
  <p>Surely it must be that (as Jon Skeet suggested) this is just .NET 1.x advice that hasn't died out, as it should have done in 2005?</p>
  <hr />
  <p><strong>UPDATE</strong></p>
  <p>As of C# 6, <a href="https://stackoverflow.com/a/45949862/3195477">the answer</a> to this question is:</p>
  <pre><code>SomeEvent?.Invoke(this, e);
  </code></pre>