Possible Duplicate:
How to correctly unregister an event handler
MSDN states the following two event subscriptions are exactly equivalent (C# 2.0 vs 1.0 syntax):
publisher.CustomEvent += HandleCustomEvent;
publisher.CustomEvent += new CustomEventHandler(HandleCustomEvent);
I note that the newer syntax hides instantiation of a delegate object.
Do I need to keep a reference to a delegate so that I can properly unsubscribe later?
// 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;
Or, is this the same thing?
publisher.CustomEvent += HandleCustomEvent;
...
publisher.CustomEvent -= HandleCustomEvent;
If they are the same, why?
Does -= HandleCustomEvent
also create a new()
? If so, isn't this object different than the object created by += HandleCustomEvent
?