C# – what is a proper way to register eventHandler

cnet

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)?

Best Answer

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.

Related Topic