Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is event null not handled by delegate

Tags:

c#

null

delegates

When calling a delegate you always have to check if it is not null. This is an often cause for errors. Since delegates are more or less just a list of functions, I would assume, that this could have been easily checked by the delegate itself.

Does anybody know, why it has been implemented as it is?

like image 555
martin Avatar asked Oct 19 '10 07:10

martin


1 Answers

This may be stating the obvious, but you can declare your event to point to a no-op handler and then you don't need to check for null when invoking.

public event EventHandler MyEvent = delegate { };

Then you can call the handlers pointed to by MyEvent without checking for null.

like image 58
Brian Rasmussen Avatar answered Oct 13 '22 10:10

Brian Rasmussen