2

From the content of the answer to this question I learned a new trick; to add a trivial handler to an event in order to avoid null checking when it is raised.

public static event EventHandler SomeEvent = delegate {};

and to invoke it without the null checking:

SomeEvent(null,EventArgs.Empty);

Does this add significant overhead? If not, why isn't something like this built in?

Community
  • 1
  • 1
NominSim
  • 8,447
  • 3
  • 28
  • 38

1 Answers1

2

Does this add significant overhead? If not, why isn't something like this built in?

It doesn't add significant overhead - merely a delegate call when the event is raised.

As for why it's not built-in - there are a couple of downsides:

  1. This isn't necessarily bullet proof - you can still clear the handler list afterwards, in which case, you'd still need the proper checking.
  2. This does add overhead - while minor, that overhead could be problematic in specific scenarios.
Community
  • 1
  • 1
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • One would think that if they were to build in that functionality that clearing the handler list would leave the trivial one behind. Could you provide an example of a scenario where the added overhead would be problematic? – NominSim May 18 '12 at 20:48
  • @NominSim Sockets and timers come to mind - in those scenarios, you wouldn't want any extra overhead... – Reed Copsey May 18 '12 at 21:03
  • Well you'll have overhead either way, either checking against null or calling a trivial delegate, which is more? – NominSim May 18 '12 at 21:07
  • @NominSim null check would be lower overhead than the delegate call - and still required (as the handler list could be cleared) – Reed Copsey May 18 '12 at 21:10
  • I just wrote a trivial program to check, the delegate call takes 1.45 times longer than the null check when performed in method, but when a method is called (as in most standards that I have seen) i.e. `OnSomeEvent(){ if (SomeEvent != null){ SomeEvent(this, EventArgs.Empty);}}` the null checking takes 2.48 times longer than the delegate. – NominSim May 18 '12 at 21:45
  • Oh I meant also to say that if it were a built in feature, I would expect that they would make a special case for when the handler is cleared to simply add another trivial delegate. – NominSim May 18 '12 at 21:47
  • @NominSim That would require a CLR change, in which case, they could just do the null check, instead ;) – Reed Copsey May 18 '12 at 23:01
  • @NominSim As for your timings - thats' actually not the standard - you should make a temporary for the handler, and check against (and use) it - I suspect, with an empty case, the null check will still be faster, if profiled correctly. In any case, it's still "correct" given the language and CLR. – Reed Copsey May 18 '12 at 23:02
  • That is how I wrote it, the program was faster with the null check when I didn't add an additional method call, but when I did it was slower. I'm sure I am probably doing something wrong though, thank you btw for humoring me with this. – NominSim May 19 '12 at 10:13