Difference between wiring events using “new EventHandler” and not using new EventHandler"?

后端 未结 4 820
情书的邮戳
情书的邮戳 2021-01-18 10:06

What\'s the difference between the two?

object.ProgressChanged += new EventHandler(object_ProgressChanged)

object.ProgressCh         


        
相关标签:
4条回答
  • 2021-01-18 10:48

    No difference. The same ilcode is generated.

    As for which one is better: I use the second options since it's cleaner code = easier to read.

    0 讨论(0)
  • 2021-01-18 10:58

    There is no difference between the two, they are same. In fact, the latter is just a shortcut and it will be compiled like the former.

    Riana

    0 讨论(0)
  • 2021-01-18 11:00

    No difference. The second one will be implicitely converted to the first by the compiler.

    0 讨论(0)
  • 2021-01-18 11:01

    Basically, one is shorter than the other. It's just synctactic sugar.

    The "correct" syntax is the first one, as ProgresChanged is an EventHandler event, so for you to assign a actual handler to it, you need to create a new EventHandler object, whose constructor takes as a parameter the name of a method with the required signature.

    However, if you just specify the name of the method (second syntax), an instance of the eventHandler class is created implicitly, and that instance is assigned to the ProgressChanged event.

    I prefer using the second method because it's shorter, and does not lose any information. There are not much contexts where you could mistake a += methodName construct for something else.

    0 讨论(0)
提交回复
热议问题