开发者

What's the difference between a delegate creation expression and a method group conversion?

开发者 https://www.devze.com 2023-02-04 01:36 出处:网络
I was wondering what the diffe开发者_开发技巧rence was between button.Click += new RoutedEventHandler(button_Click);

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号