This question already has an answer here:
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.
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:
From what I understand of this answer, the first is not thread-safe whereas the second is.
protected virtual void OnNumberReached(NumberReachedEventArgs e)
{
//If number reached is changed from after this check
if(NumberReached != null)
{
//and between this call, it could still result in a
//NullReferenceException
NumberReached(this, e);
}
}