具有和不具有“新”的布线事件之间的差异

编程入门 行业动态 更新时间:2024-10-09 01:14:51
本文介绍了具有和不具有“新”的布线事件之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在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

更多推荐

具有和不具有“新”的布线事件之间的差异

本文发布于:2023-11-25 20:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631321.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:差异   事件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!