Does assigning null remove all event handlers from an object?

Dor Cohen picture Dor Cohen · Jan 17, 2012 · Viewed 21.9k times · Source

I have defined new member in my class

protected COMObject.Call call_ = null;

This class has the following event handler that I subscribed to

call_.Destructed += new COMObject.DestructedEventHandler(CallDestructedEvent);

Will setting my member to null as following remove the event handler?

call_ = null;

or I have to unsubscribed with -=?

Answer

Azodious picture Azodious · Jan 17, 2012

yes, you should use overloaded -= to unsubscribe an event.

simply assigning a reference to null will not do that automatically. The object will still be listening to that event.