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 开发者_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.
精彩评论