简体   繁体   English

连接事件处理程序

[英]Wiring EventHandlers

Is there a difference between有没有区别

Object.Event += new System.EventHandler(EventHandler);
Object.Event -= new System.EventHandler(EventHandler);

AND

Object.Event += EventHandler;
Object.Event -= EventHandler;

? ? If so, what?如果是这样,是什么?

Aren't they both just pointers to methods?它们不都是指向方法的指针吗?

Both are Exactly same.两者完全相同。 But

Object.Event += EventHandler;
Object.Event -= EventHandler;

The above example compiles fine only in 3.0 or later version of C#, while if you are in 2.0 or before you can only use following construct.上述示例仅在 C# 的 3.0 或更高版本中编译良好,而如果您在 2.0 或之前版本,则只能使用以下构造。

Object.Event += new System.EventHandler(EventHandler);
Object.Event -= new System.EventHandler(EventHandler);

Look more about at Type inferencing .更多地了解类型推断 search for "Type Inference"搜索“类型推断”

No, they are exactly the same.不,它们完全一样。 The second version is purely a shorthand where the compiler creates an instance of the event handler for you.第二个版本纯粹是一种简写,编译器为您创建事件处理程序的实例。 Just like simplified property syntax, using etc... all compiler magic!就像简化的属性语法一样,使用等等……所有的编译器魔法!

See this related question:请参阅此相关问题:

Difference between wiring events using "new EventHandler<T>" and not using new EventHandler<T>"? 使用“new EventHandler<T>”和不使用“new EventHandler<T>”连接事件的区别?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM