What is the difference between a delegate creation expression and a method group transformation?

I was wondering what is the difference between

button.Click += new RoutedEventHandler(button_Click);

and

button.Click += button_Click;

I saw both, and, as a rule, used the 2nd version, but I was wondering what the difference is and when to use it over another.

+3
source share
1 answer

In C # 1 there is no difference other than the first, and the second is not.

On the other hand, the difference in terms is only RHS expressions. For instance:

Delegate valid = new RoutedEventHandler(button_Click);
Delegate invalid = button_Click;

In the latter case, the compiler does not know which delegate you want to convert to a group of methods, so you will be denied compilation.

+5
source

Source: https://habr.com/ru/post/1785463/


All Articles