Difference in event subscription formatting

对着背影说爱祢 提交于 2019-12-17 21:16:10

问题


Is there a difference between these two formats for subscribing to events:

Style 1:

foo.BarEvent += FooEventMethod;

Style 2:

foo.BarEvent += new FooEventHandler(FooEventMethod);

回答1:


This is c# 1.0 style of subscribing an event.

foo.BarEvent += new FooEventHandler(FooEventMethod);

Starting from c# 2.0 you're allowed to subscribe event like this

foo.BarEvent += FooEventMethod;

above code is exactly equal to version1 code, what happens is compiler will create new FooEventHandler(FooEventMethod) in background for you.



来源:https://stackoverflow.com/questions/23708421/difference-in-event-subscription-formatting

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!