可能重复:
如何正确注销事件处理程序
MSDN 声明以下两个事件订阅完全相同(C# 2.0 与 1.0 语法):
publisher.CustomEvent += HandleCustomEvent;
publisher.CustomEvent += new CustomEventHandler(HandleCustomEvent);
我注意到较新的语法隐藏了委托对象的实例化。
我是否需要保留对代表的引用,以便以后可以正确退订?
// Retain reference to delegate used to subscribe.
this.handleCustomEvent = new CustomEventHandler(HandleCustomEvent);
publisher.CustomEvent += this.handleCustomEvent;
...
// Use earlier reference to unsubscribe.
publisher.CustomEvent -= this.handleCustomEvent;
或者,这是同一件事吗?
publisher.CustomEvent += HandleCustomEvent;
...
publisher.CustomEvent -= HandleCustomEvent;
如果它们相同,为什么?
是否-= HandleCustomEvent
还创建一个new()
?如果是这样,这个对象是否与创建的对象不同+= HandleCustomEvent
?