关于c#:这两种连接事件处理程序的语法有什么区别? | 珊瑚贝

What is the difference between these two syntaxes for hooking up an event handler?

本问题已经有最佳答案,请猛点这里访问。


Possible Duplicate:
Is there an actual difference in the 2 different ways of attaching event handlers in C#?

我见过很多这样的代码:

1
foo.Drop += new DragEventHandler(fooHandler);

但在过去,我总是这样做:

1
foo.Drop += fooHandler;

这两种语法有区别吗?如果是这样,长期这样做有什么好处吗?

  • 在 C# 中附加事件处理程序的 2 种不同方式是否存在实际差异?,有委托处理程序的事件和没有委托处理程序的事件有什么区别?,附加\\\\\\\\分离事件的不同方式有什么区别? C# 中的处理程序
  • 绝对是重复的。投票关闭自己。


第二个是第一个的简写;它们将编译为相同的 IL。

但是,第二种语法是 C# 2.0 的新语法; C# 1 只支持第一个。


它们都会产生相同的 IL。

所以,在回答您的问题时,不 – 使用更长的版本没有任何好处。

  • 您链接到一个消歧页面,该页面表明 MSIL 是现在称为 CIL 的”前首字母缩略词”。
  • @Cody Gray 更新为更直接的链接
  • 我认为这是为了强调该平台不是完全专有的。微软在不同时期都对 Mono Project 等工作给予了鼓励。我认识的每个人都称它为”IL”,包括我自己。防止营销干扰理解。 ;-)
  • 好地方-谢谢。答案更新为”IL”;)


它们是相同的,但在第二个示例中,编译器使用方法组转换来为您推断委托类型。语法糖…


没有区别,因为 .Net 2 并且您可以使用所谓的方法组转换,它允许您将方法名称直接注册到事件而无需委托 Object


来源:https://www.codenong.com/5951009/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9368.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?