3

Possible Duplicate:
Attaching Eventhandler with New Handler vs Directly assigning it

What is the difference between assigning a callback to, lets say a button's Click event by using += new(...) versus just +=? Here are samples of each for clarity:

Button b = new Button();
b.Click += new System.EventHandler(button_Click);
b.Click += button_Click;

Does the first one create a new instance of the method button_Click whereas the second always uses the one defined in this?

Community
  • 1
  • 1
Jan Tacci
  • 3,131
  • 16
  • 63
  • 83

2 Answers2

7

The second one is short hand for the first one, so both will create the event handler and add it to Click.

Here's a good explanation from the chapter on events in "C# in Depth."

DavidRR
  • 18,291
  • 25
  • 109
  • 191
Ian
  • 4,885
  • 4
  • 43
  • 65
2

There is no difference.

You could also do..

b.Click += (e, sender) =>{
 // do something here
};

All three are the same, i.e. assigning a function to a delegate.

Robin Maben
  • 22,194
  • 16
  • 64
  • 99