Is there a downside to adding an anonymous empty delegate on event declaration?(在事件声明中添加匿名空委托有缺点吗?)
问题描述
我看到有人提到过这个成语(包括关于 SO):
I have seen a few mentions of this idiom (including on SO):
// Deliberately empty subscriber
public event EventHandler AskQuestion = delegate {};
好处很明显 - 它避免了在引发事件之前检查 null 的需要.
The upside is clear - it avoids the need to check for null before raising the event.
但是,我很想知道是否有任何缺点.例如,它是否被广泛使用并且足够透明以至于不会引起维护问题?空事件订阅者调用是否有任何明显的性能影响?
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.
这篇关于在事件声明中添加匿名空委托有缺点吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在事件声明中添加匿名空委托有缺点吗?
基础教程推荐
- SSE 浮点算术是否可重现? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01