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

Categories

0 votes
207 views
in Technique[技术] by (71.8m points)

c# - Difference in event subscription formatting

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

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

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...