1

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?

Community
  • 1
  • 1
Kevin P. Rice
  • 5,550
  • 4
  • 33
  • 39

2 Answers2

1

This is the exact same thing I think, you just focused on the second, short-hand syntax bit.

How to correctly unregister an event handler

I wouldn't answer on that just, but I wanted to add this...

if you'd like you might want to take a look at Rx - Reactive Extensions which frees you of these very problems. Unsubscribe is basically not mandatory unless you would want to 'stop' the event sooner (this is simplified, there are more details to this)

Community
  • 1
  • 1
NSGaga-mostly-inactive
  • 14,052
  • 3
  • 41
  • 51
  • Thank you! Exactly what I was looking for. Now I can delete all the pesky private fields I didn't need in the first place. – Kevin P. Rice Apr 08 '12 at 01:06
1

Does -= HandleCustomEvent also create a new()? If so, isn't this object different than the object created by += HandleCustomEvent?

Yes and yes.

As far as I can tell, the MSDN documentation for Delegate.Remove does not specifically explain how it determines whether two delegates are equal. However, MulticastDelegate.RemoveImpl appears to use Delegate.Equals to determine equality, and that is documented:

The methods and targets are compared for equality as follows:

  • If the two methods being compared are both static and are the same method on the same class, the methods are considered equal and the targets are also considered equal.
  • If the two methods being compared are instance methods and are the same method on the same object, the methods are considered equal and the targets are also considered equal.
  • Otherwise, the methods are not considered to be equal and the targets are also not considered to be equal.

So even though the delegate passed to -= is not the same instance as the delegate passed to +=, the event will still be successfully unsubscribed.

Michael Liu
  • 52,147
  • 13
  • 117
  • 150
  • Thank you for pointing out the documentation for `Delegate.Equals`. This makes perfect sense and I assumed it to be true, but assumptions lead to bugs... – Kevin P. Rice Apr 08 '12 at 01:15