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.
source share