开发者

Why "new EventHandler(Handler)" in .Net?

In VisualStudio 2008 and 2010, when I type, e.g.

this.Activated += 

and press Tab, VisualStudio automatic开发者_如何学运维ally complements the line:

this.Activated += new EventHandler(MainWindow_Activated);

But the simpler, more laconic versions works too:

this.Activated += MainWindow_Activated;

Are these lines equivalent? new EventHandler is probably there for a reason...


Newer versions of C# infer the long version when you use the short version. This means even though you write less code, it still compiles to the longer version.


Jon Skeet wrote in his Refcard:

C# 2

C# 2 introduced two important improvements in the ways we can create delegate instances.

  1. You no longer need the new delegate-type part:

StringPredicate predicate = fveCharacters.Filter;

meaning as of C# 2, you can use the above instead of

StringPredicate predicate = new StringPredicate(fveCharacters.Filter);

So I'm guessing Visual Studio puts it there for backwards-compatibility.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜