简体   繁体   中英

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.

The second option is preferred by many - including ReSharper - because it is easier to read and less code. The generated IL code however is the same, so it is purely a question of preference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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