0

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...... }
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Cos
  • 552
  • 1
  • 9
  • 16
  • possible duplicate of [Is there an actual difference in the 2 different ways of attaching event handlers in C#?](http://stackoverflow.com/questions/214346/is-there-an-actual-difference-in-the-2-different-ways-of-attaching-event-handlers) – Fredrik Mörk Feb 15 '11 at 11:24
  • possible duplicate of [C# Event handlers](http://stackoverflow.com/questions/26877/c-sharp-event-handlers) – nawfal Jul 06 '14 at 20:38

1 Answers1

4
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.

Kamal
  • 2,512
  • 2
  • 34
  • 48