与无&QUOT布线事件之间的区别;新" [英] Difference between wiring events with and without "new"
问题描述
在C#,code的这两条线之间的差值(如果有的话)是什么?
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在code。
所产生的MSIL是完全一样的。
And then ran ildasm over the code.
The generated MSIL was exactly the same.
因此,要回答你的问题,是的,他们是同一个东西。
编译器只是推断要 someEvent + =新的EventHandler(Program_someEvent);
- 你可以看到它在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
这篇关于与无&QUOT布线事件之间的区别;新"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!