开发者

attaching event handlers

开发者 https://www.devze.com 2023-02-10 08:13 出处:网络
Can anyone clarify to me the difference between the following: 1. { // ... Button b = new Button(); b.Click += new RoutedEventHandler(b_Click);

Can anyone clarify to me the difference between the following:

1.

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

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

2.

{
    // ...
    Button b = new Button();
    开发者_如何学Gob.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);

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.

It's a personal preference as to how you want the code to look to the programmer.

0

精彩评论

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