Checking if the handler != null vs Checking if the event != null in C#
c#, delegates, events, handler, null
Solution
Both are checking to see if the delegate associated with the event is null.
The purpose of storage into the local is to prevent a TOCTOU-style race in multithreaded code.
It is important to note that using a local only eliminates one of two potential races. See my 2009 article on the subject for details: http://blogs.msdn.com/b/ericlippert/archive/2009/04/29/events-and-races.aspx
and also this question:
C# Events and Thread Safety
Problem
I have seen various coding styles to fire events in C#. The first style consisting of the following: -an event handler ``` public delegate void NumberReachedEventHandler(object sender, NumberReachedEventArgs e); ``` -an event ``` public event NumberReachedEventHandler NumberReached; ``` -and the method to fire the event ``` protected virtual void OnNumberReached(NumberReachedEventArgs e) { if(NumberReached != null) { NumberReached(this, e); } } ``` The second style however, has a different method to fire the event: ``` protected virtual void OnNumberReached(NumberReachedEventArgs e) { NumberReachedEventHandler handler = NumberReached; if(handler != null) { handler(this, e); } } ``` To me, it appears that one style checks if the "event" is null and the second style checks if the delegate is null. However, my understanding is that an event is just an instance of a delegate, so I am wondering if there are any advantages to either way of writing the code. If so, please explain. Thanks in advance.