Problem
If you don’t place a ‘not null’ condition before raising an event, you might get a NullReferenceException. That’s why we usually type something like:
event EventHandler MyEvent;
void RaiseMyEvent()
{
if (MyEvent!= null) MyEvent(this, EventArgs.Empty);
}
Solution
Why not use the following pattern:
event EventHandler MyEvent = delegate { };
void RaiseMyEvent()
{
MyEvent(this, EventArgs.Empty);
}
This way we can avoid the following:

Comments