简体   繁体   中英

Is there an actual difference in the 2 different ways of attaching event handlers in C#?

In C# is there any real difference (other than syntax) under the hood between:

myButton.Click += new EventHandler(myMemberMethod);

and

myButton.Click += myMemberMethod;

?

The second method is a shortcut to the first one, it was introduced in C# 2.0

See also this thread .

They are exactly the same, its called syntax sugar.

There are a lot of things that arent needed, to get a better idea of them while programming you should try something like Resharper . It will color the unnecessary code in Grey. Not to mention a whole myriad of incredible tools and refactorings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM