是否有一个缺点事件声明添加匿名空委托? [英] Is there a downside to adding an anonymous empty delegate on event declaration?

查看:148
本文介绍了是否有一个缺点事件声明添加匿名空委托?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过几个提到这个成语的(包括SO ):

I have seen a few mentions of this idiom (including on SO):

// Deliberately empty subscriber
public event EventHandler AskQuestion = delegate {};

好处是明显的 - 它避免了引发事件之前,检查空

The upside is clear - it avoids the need to check for null before raising the event.

不过,我很希望了解是否有任何缺点。例如,它是东西是在wides $ P $垫的使用和足够透明的,它不会造成保养头痛?有没有空的情况下用户呼叫的任何接入点preciable性能影响?

However, I am keen to understand if there are any downsides. For example, is it something that is in widespread use and is transparent enough that it won't cause a maintenance headache? Is there any appreciable performance hit of the empty event subscriber call?

推荐答案

唯一的缺点是,你正在调用额外的空委托一个很轻微的性能损失。除此之外,有没有维修处罚或其他缺点。

The only downside is a very slight performance penalty as you are calling extra empty delegate. Other than that there is no maintenance penalty or other drawback.

这篇关于是否有一个缺点事件声明添加匿名空委托?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆