Is there a reason to assign an event to a local variable before raising it?

I often see the code as shown below and wonder if there is a reason to use a local variable for the event, and not just use the event itself. There is?

var handler = OnQueryComplete; if (handler != null) handler(this, new RepositoryEventArgs<T>(results)); 
+3
source share
1 answer

Yes, absolutely - this makes invalidation checking safe.

If you only have:

 // Bad code, do not use if (OnQueryComplete != null) { OnQueryComplete(this, ...); } 

then the last subscriber can unsubscribe between check and call, causing a NullReferenceException .

There are many options here:

  • Decide that you do not care about thread safety, even if this throws an exception. (In many cases, this may be reasonable, especially if this is all your own code base.)
  • Use the extension method to make it simple so that validation is gone anyway.
  • In C # 6, use the null conditional operator:

     OnQueryComplete?.Invoke(this, ...); 
  • Configure the event with a single empty handler that is never deleted:

     public event FooEventHander OnQueryComplete = delegate {}; 

You can also use Interlocked to make sure you have the last variable value to avoid memory model issues. See my blog post (including comments) for a more in-depth discussion of this issue.

+9
source

All Articles