带有和不带有“new"的接线事件之间的区别

编程入门 行业动态 更新时间:2024-10-27 04:30:12
本文介绍了带有和不带有“new"的接线事件之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在 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 );-- 您可以在 MSIL 中看到它在两种情况下都创建了新的 EventHandler 对象

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

更多推荐

带有和不带有“new"的接线事件之间的区别

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

发布评论

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

>www.elefans.com

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