开发者

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

开发者 https://www.devze.com 2022-12-18 15:36 出处:网络
Let\'s say I have a collection of thousands of objects, all of which implement the following: public event EventHandler StatusChanged = (s,e) => {};

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 开发者_C百科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?


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号