0

I have just come across this code in a book I'm working through:

public event EventHandler<ProjectEventArgs> ProjectUpdated = delegate { };

Is setting a delegate here merely to ensure ProjectUpdated is never null, and this avoid the standard null check before firing the event?

leppie
  • 115,091
  • 17
  • 196
  • 297
ProfK
  • 49,207
  • 121
  • 399
  • 775
  • 3
    Yes, that is exactly why. – Peter Duniho Jan 26 '15 at 04:48
  • 2
    See also [Is there a downside to adding an anonymous empty delegate on event declaration?](http://stackoverflow.com/questions/170907/is-there-a-downside-to-adding-an-anonymous-empty-delegate-on-event-declaration) – Peter Duniho Jan 26 '15 at 04:51

1 Answers1

0

Normally, when you are to raise an event, you have to check for null values, meaning, no event handlers attached. With this approach, you can skip this check, because the event will always have an empty handler.

Ricardo Peres
  • 13,724
  • 5
  • 57
  • 74