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,061
  • 16
  • 57
  • 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
  • 16,676
  • 19
  • 101
  • 178
Ian
  • 4,825
  • 4
  • 40
  • 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
  • 20,994
  • 16
  • 62
  • 96