Is it safe to add delegates to events with keyword new?

后端 未结 4 472
时光取名叫无心
时光取名叫无心 2021-01-12 20:02

One thing I am concerned with is that I discovered two ways of registering delegates to events.

  1. OnStuff += this.Handle;
  2. OnStuff += new StuffEventHand
4条回答
  •  温柔的废话
    2021-01-12 20:56

    Number one is just shorthand that will generate the same MSIL as the 2nd one, at compile type it'll look at this.Handle and infer the delegate to instantiate. But you should never unsubscribe by using new.

    So there is no difference between the 2, just some syntactic sugar to make our code cleaner.

提交回复
热议问题