开发者

Subscribe to events

开发者 https://www.devze.com 2023-03-20 18:18 出处:网络
There are two options for subscribing to events: this.button1.click += new System.EventHandler(this.button1_Click) 开发者_如何学编程

There are two options for subscribing to events:

  1. this.button1.click += new System.EventHandler(this.button1_Click)
  2. 开发者_如何学编程
  3. 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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消