Is there a benefit to explicit use of “new EventHandler” declaration?

前端 未结 4 1057
隐瞒了意图╮
隐瞒了意图╮ 2021-01-12 04:28

In assigning event handlers to something like a context MenuItem, for instance, there are two acceptable syntaxes:

MenuItem item = new MenuItem(         


        
4条回答
  •  被撕碎了的回忆
    2021-01-12 05:15

    In C# 1.0 you had no choice but to explicitly define the delegate type and the target.

    Since C# 2.0 the compiler allows you to express yourself in a more succinct manner by means of an implicit conversion from a method group to a compatible delegate type. It's really just syntactic sugar.

    Sometimes you have no choice but to use the long-winded syntax if the correct overload cannot be resolved from the method group due to an ambiguity.

提交回复
热议问题