简体   繁体   中英

attaching event handlers

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();
    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);

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.

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