Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I was showing some of my code to a junior programer, and he asked me why am I registering functions for event handling like this:

button1.Click += new RoutedEventHandler(button1_Click);

and not like this:

button1.Click += button1_Click;

The only thing I could say was: because you just write += and press tab two times...

What is the difference between those two metods (if there is a difference)?

share|improve this question
1  
I would normally migrate this to Stack Overflow, however I know for a fact that it would simply be a duplicate there. However, I think it's OK for here too. –  ChrisF Feb 17 '11 at 12:40
    
thx. I assumed answer will be that its thing of taste. so i posted it here. –  grabah Feb 17 '11 at 13:03
1  
In VS2012, it automatically creates the second one now. –  DaveShaw Mar 7 '13 at 16:47

2 Answers 2

up vote 6 down vote accepted

The answer depends on what version of C# you are using.

The early versions required the new RoutedEventHandler while the newer versions don't.

They compile to the same code.

share|improve this answer

Personally, if what you're doing in the handler is really short and not shared with any other events, I like this form:

button1.Click += (sender, e) =>
{
    DoSomethingAwesome(e.RoutedEvent);
    e.Handled = true;
};
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.