简体   繁体   English

附加事件处理程序

[英]attaching event handlers

Can anyone clarify to me the difference between the following: 任何人都可以向我澄清以下两者之间的区别:

1. 1。

{
  // ... 
  Button b = new Button(); 
  b.Click += new RoutedEventHandler(b_Click);
}

void b_Click(object sender, RoutedEventArgs e) { //do stuff...... }

2. 2。

{
    // ...
    Button b = new Button();
    b.Click += a_Click;
}

void a_Click(object sender, RoutedEventArgs e) { //do stuff...... }
b.Click += a_Click;

is simply a shorthand of writing b.Click += new RoutedEventHandler(b_Click); 只是编写b.Click += new RoutedEventHandler(b_Click);的简写b.Click += new RoutedEventHandler(b_Click);

If you write the short form, behind the scenes the compiler will generate the long version. 如果编写简短形式,则编译器将在后台生成长版本。 In other words, whichever way you choose, the code being executed will be the same at the IL level. 换句话说,无论选择哪种方式,执行的代码在IL级别都是相同的。

It's a personal preference as to how you want the code to look to the programmer. 对于您希望代码如何呈现给程序员,这是个人喜好。

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

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