initialization of events that looked something like this;
public event EventHandler EmptyDelegateEvent = delegate { };
I mean; I do know my way around delegates, but these empty delegates puzzled me. After googling around a bit I found that this was a just a clever way of avoiding null reference exception when raising the event. So instead of this;
public event EventHandler CheckForNullEvent;
void RaiseEvent()
{
// the usuall way
if (CheckForNullEvent != null)
CheckForNullEvent(this, new EventArgs());
}
...you can simply do this;
public event EventHandler EmptyDelegateEvent = delegate { };
void RaiseEvent()
{
// the easy way
EmptyDelegateEvent(this, new EventArgs());
}
When using empty delegates you can simply raise the event without checking for null. Note though, that performance wise it will be better to do the "if not null" check than a "delegate {}". But I ran some performance testing to compare the two, and the performance hit is really not that big of an issue. Even though the performance of the empty delegate was 20-30% slower than the null check, the time it takes to do either of them is miniscule. This is really not where you should put your performance tuning effort. A 'null reference exception', which is much more likely to happen if you forget to check for the null, will have a much bigger impact on the overall performance of your application. Or even worse; if a raise condition occurs. Which again reminds me that the correct way of checking the event for null and at the same time making sure it's thread-safe, is this;
void RaiseEvent()
{
// the thread-safe way
EventHandler copyOfEvent = CheckForNullEvent;
if (copyOfEvent != null)
copyOfEvent(this, new EventArgs());
}
And how many does actually do that? And talking of performance; what about 'developer performance'? Instantiating an empty delegate and no need for null checking or possible raise conditions, means faster coding and less bugs - e.g. better developer performance.