具有和不具有“新”的布线事件之间的差异 [英] Difference between wiring events with and without "new"

查看:165
本文介绍了具有和不具有“新”的布线事件之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,这两行代码之间有什么区别(如果有)?

In C#, what is the difference (if any) between these two lines of code?

tmrMain.Elapsed += new ElapsedEventHandler(tmrMain_Tick);

tmrMain.Elapsed += tmrMain_Tick;

两者似乎工作正常。 C#只是假设你的意思是前者,当你输入后者?

Both appear to work exactly the same. Does C# just assume you mean the former when you type the latter?

推荐答案

我这样做

static void Hook1()
{
    someEvent += new EventHandler( Program_someEvent );
}

static void Hook2()
{
    someEvent += Program_someEvent;
}

然后在代码上运行ildasm。

生成的MSIL是完全相同的。

And then ran ildasm over the code.
The generated MSIL was exactly the same.

所以要回答你的问题,是的,他们是一样的。

编译器只是推断你想要 someEvent + = new EventHandler(Program_someEvent);

- 您可以看到它创建新的 EventHandler 对象在这两种情况下,MSIL

So to answer your question, yes they are the same thing.
The compiler is just inferring that you want someEvent += new EventHandler( Program_someEvent );
-- You can see it creating the new EventHandler object in both cases in the MSIL

这篇关于具有和不具有“新”的布线事件之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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