NominSim
NominSim

Reputation: 8511

Does adding trivial handler to event cause significant overhead?

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?

Upvotes: 2

Views: 274

Answers (1)

Reed Copsey
Reed Copsey

Reputation: 564433

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.

Upvotes: 2

Related Questions