5

In C#, when registering a event handler, you have two options (btn is of type System.Windows.Controls.Button):

btn.Click += new RoutedEventHandler (ButtonClick) and

btn.Click += ButtonClick.

What's the semantic difference between them and their implications?

Daniel Albuschat
  • 806
  • 8
  • 20

3 Answers3

7

The first version will compile without errors on all versions of .Net.

The second version will only compile on .Net 2 or later.

And that's the only difference. The second version is just some syntactic sugar introduced with .Net 2.

It's known as Method Group Conversion. See here for details:

http://mike-ward.net/blog/post/00020/anonymous-methods-method-group-conversions-and-eventhandler

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
3

As I understood it, nothing, the compiler will infer the delegate type and wrap it for you automatically, it's just a shorthand way of doing it (because who really wants to type it all out).

Lloyd
  • 29,197
  • 4
  • 84
  • 98
  • basically, from what I just red, the syntax with new keyword works for framework 1.1, without it, it will give compiler error, that's why it is the default one in intellisense. – sameh.q Jun 18 '13 at 09:22
  • Yes I recall, because of that I tend to always do the long way. – Lloyd Jun 18 '13 at 09:27
1

There is no differences. To understand these C# feature, you should read C# delegate and event keyword.

min
  • 29
  • 2