简体   繁体   English

订阅活动

[英]Subscribe to events

There are two options for subscribing to events:订阅事件有两种选择:

  1. this.button1.click += new System.EventHandler(this.button1_Click)
  2. this.button1.click += this.button1_click

What are the advantages and disadvantages of both techniques?这两种技术的优缺点是什么?

The second one is easier to read;-) - they both work in the same way.第二个更容易阅读;-) - 它们都以相同的方式工作。

Second option is only syntactic sugar for the first one.第二个选项只是第一个选项的语法糖。

You can check generated IL-code and you'll see that compiler generates exactly the same IL-code for both cases.您可以检查生成的 IL 代码,您会看到编译器为这两种情况生成完全相同的 IL 代码。

The second option is preferred by many - including ReSharper - because it is easier to read and less code.许多人(包括 ReSharper)更喜欢第二个选项,因为它更易于阅读且代码更少。 The generated IL code however is the same, so it is purely a question of preference.然而,生成的 IL 代码是相同的,所以这纯粹是一个偏好问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM