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();
开发者_如何学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.
精彩评论