使用新处理程序附加事件处理程序与直接分配它 [英] Attaching Eventhandler with New Handler vs Directly assigning it
问题描述
创建新的事件处理程序与直接将其分配给事件的实际区别、优点和缺点是什么?
What is the actual difference, advantages and disadvantages, of creating a new event handler, vs assigning it directly to the event?
_gMonitor.CollectionChanged += new NotifyCollectionChangedEventHandler(OnCollectionChanged);
对比
_gMonitor.CollectionChanged += OnCollectionChanged;
推荐答案
在 C# 2.0 及更高版本中,它们是相同的.在 C# 1.2(.NET 1.1 附带的那个)中,只有第一个语法(使用 new
)编译;-p
In C# 2.0 and above, they are identical. In C# 1.2 (the one that shipped with .NET 1.1), only the first syntax (with new
) compiles ;-p
第二种语法可以节省按键次数,但 VS 智能感知通常会建议第一种.最终,它几乎没有什么区别.我通常在在线代码示例中使用第二种语法,只是因为它避免超过(窄)列宽!
The second syntax saves key presses, but VS intellisense will typically suggest the first. Ultimately, it makes very little difference. I generally use the second syntax in code-samples online, simply because it avoids going over the (narrow) column width!
这篇关于使用新处理程序附加事件处理程序与直接分配它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!