What's the difference between a delegate creation expression and a method group conversion?
I was wondering what the diffe开发者_开发技巧rence was between
button.Click += new RoutedEventHandler(button_Click);
and
button.Click += button_Click;
I've seen both and generally use the 2nd version, but I was wondering what the difference was and when to use one over the other.
There's no difference, other than the first is valid in C# 1 and the second isn't.
There's a difference in terms of just the RHS expression, on the other hand. For example:
Delegate valid = new RoutedEventHandler(button_Click);
Delegate invalid = button_Click;
In the latter case the compiler doesn't know which delegate you want to convert the method group into, so you'll get a compile-time failure.
精彩评论