QUOT&;吨GT&;新的EventHandler<采用QUOT接线事件与区别;并没有使用新的EventHandler< T>"? [英] Difference between wiring events using "new EventHandler<T>" and not using new EventHandler<T>"?
问题描述
什么是两者之间的区别
object.ProgressChanged += new EventHandler<ProgressChangedEventArgs>(object_ProgressChanged)
object.ProgressChanged += object_ProgressChanged;
void installableObject_InstallProgressChanged(object sender, ProgressChangedEventArgs e)
{
EventHandler<ProgressChangedEventArgs> progress = ProgressChanged;
if (installing != null)
installing(this, e);
}
编辑:
如果没有差异,这是更好的选择?
If there are no difference, which is the better choice?
谢谢!
推荐答案
基本上,一个是比另一个要短。这只是synctactic糖。
Basically, one is shorter than the other. It's just synctactic sugar.
正确的语法是第一位的,因为ProgresChanged是一个事件处理程序的事件,所以你到一个实际的处理器分配给它,你需要创建一个新的EventHandler对象,其构造函数作为参数与要求签名的方法的名称。
The "correct" syntax is the first one, as ProgresChanged is an EventHandler event, so for you to assign a actual handler to it, you need to create a new EventHandler object, whose constructor takes as a parameter the name of a method with the required signature.
不过,如果你只是指定方法的名称(第二个语法),该事件处理程序类的一个实例是隐式创建,该实例分配给ProgressChanged事件。
However, if you just specify the name of the method (second syntax), an instance of the eventHandler class is created implicitly, and that instance is assigned to the ProgressChanged event.
我更喜欢使用第二种方法,因为它是短,不会丢失任何信息。有没有太多的上下文,你会认错一个 + =方法名
构建别的东西。
I prefer using the second method because it's shorter, and does not lose any information. There are not much contexts where you could mistake a += methodName
construct for something else.
这篇关于QUOT&;吨GT&;新的EventHandler<采用QUOT接线事件与区别;并没有使用新的EventHandler< T>"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!