Unsubscribing from events.

  • Thread starter Thread starter Pete Davis
  • Start date Start date
P

Pete Davis

I came across the following code in the .NET Framework:

((IBindingList) list).ListChanged -= new
ListChangedEventHandler(this.List_ListChanged);

In other places, they hold onto a reference to the original event handler
instance and unsubscribe that. I've always held the references and
unsubscribed the original myself.

Unsubscribing from a new instance instead of the original instance just
seems wrong. Yet one of my co-workers claims this actually works. Does it?

Pete
 
Pete,

Yes, it works. I wouldn't waste the field to hold onto this. It's the
method that is the identity of the delegate, not the instance of the
delegate itself.

Hope this helps.
 
That's what my co-worker said, but I just wanted to be sure. Thanks
Nicholas.

Pete

Nicholas Paldino said:
Pete,

Yes, it works. I wouldn't waste the field to hold onto this. It's the
method that is the identity of the delegate, not the instance of the
delegate itself.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Pete Davis said:
I came across the following code in the .NET Framework:

((IBindingList) list).ListChanged -= new
ListChangedEventHandler(this.List_ListChanged);

In other places, they hold onto a reference to the original event handler
instance and unsubscribe that. I've always held the references and
unsubscribed the original myself.

Unsubscribing from a new instance instead of the original instance just
seems wrong. Yet one of my co-workers claims this actually works. Does it?

Pete
 
Back
Top