C#:'+ = anEvent'和'+ = new EventHandler(anEvent)'之间的区别 [英] C#: Difference between ' += anEvent' and ' += new EventHandler(anEvent)'

查看:139
本文介绍了C#:'+ = anEvent'和'+ = new EventHandler(anEvent)'之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

采取以下代码:

private void anEvent(object sender, EventArgs e) {
    //some code
}






有什么区别以下之间?


What is the difference between the following ?

[object].[event] += anEvent;

//and

[object].[event] += new EventHandler(anEvent);

[更新]

显然,两者之间没有区别,前者只是后者的句法糖。

Apparently, there is no difference between the two...the former is just syntactic sugar of the latter.

推荐答案

没有区别。在第一个例子中,编译器将自动推断您想要实例化的委托。在第二个示例中,您明确定义委托。

There is no difference. In your first example, the compiler will automatically infer the delegate you would like to instantiate. In the second example, you explicitly define the delegate.

C#2.0中添加了代理推理。所以对于C#1.0项目,第二个例子是你唯一的选择。对于2.0项目,使用推理的第一个例子是我希望在代码库中使用和查看的内容,因为它更简洁。

Delegate inference was added in C# 2.0. So for C# 1.0 projects, second example was your only option. For 2.0 projects, the first example using inference is what I would prefer to use and see in the codebase - since it is more concise.

这篇关于C#:'+ = anEvent'和'+ = new EventHandler(anEvent)'之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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