Checking for Null Before Raising C# Events

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Im currently studying events in C# and Im reading through the Microsoft Press book .NET Framework 2.0 Application Development Foundation.  On page 46 of that book the author claims that before invoking an event in C# that you must check to make sure that the event isnt null.
 
Within the class that declares and raises the event the author has the following code:
 
<font face="Courier New, Courier, Monospace if (myEvent != null)</font>
<font face="Courier New, Courier, Monospace {</font>
<font face="Courier New, Courier, Monospace    myEvent(this, EventArgs.Empty);</font>
<font face="Courier New, Courier, Monospace }</font>
 
Ive running the code with a form that was listening for the event and with a form that was not listening for the event without the null check in place and in both cases it worked fine.  I cannot figure out why this null check is necessary.  Any clues here?
 
 
 

View the full article
 
Back
Top