开发者

What is the difference between these two syntaxes for hooking up an event handler? [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

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

I've been seeing a lot of code that looks like this:

开发者_Python百科
foo.Drop += new DragEventHandler(fooHandler);

But in the past, I've always done this:

foo.Drop += fooHandler;

Is there a difference between these two syntaxes? If so, is there any advantage to doing it the long way?


The second is shorthand for the first; they will compile to indentical IL.

However, the second syntax is new to C# 2.0; C# 1 only supports the first.


They will both result in the same IL.

So, in answer to your question, no - there is no benefit of using the longer version.


No difference , since .Net 2 and you can use what is called Method Group Conversion which allow you to Register the method name directly to the event without making a delegate Object


They are the same, but in the second example, the compiler uses Method Group conversion to infer the delegate type for you. Syntactic sugar...

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜