Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

Style 1:

foo.BarEvent += FooEventMethod;

Style 2:

foo.BarEvent += new FooEventHandler(FooEventMethod);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
202 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...