LJW
LJW

Reputation: 2388

Are there any performance drawbacks to using 'do nothing' default event handlers?

Let's say I have a collection of thousands of objects, all of which implement the following:

public event EventHandler StatusChanged = (s,e) => {};
private void ChangeStatus()
{
  StatusChanged(this, new EventArgs());
}

If no handlers are subscribed to that event for each object, does using the no-op event handler provide any performance drawbacks? Or is the CLR smart enough to ignore it? Or am I better off checking for a StatusChanged handler before firing the event?

Upvotes: 2

Views: 424

Answers (2)

Sergey Teplyakov
Sergey Teplyakov

Reputation: 11657

If your application calls ChangeStatus thousand times per second, maybe it would be a problem. But only profiler can prove this.

Upvotes: 1

Mehrdad Afshari
Mehrdad Afshari

Reputation: 422016

Yes, the CLR is not really smart enough to ignore it but the difference should be negligible in most cases.

A method call is not a big deal and is unlikely to have a meaningful impact on the performance of your application.

Upvotes: 2

Related Questions