When/How to release an event handler allocated in a contructor

K

Kenneth Baltrinic

I have a custom collection class that raises OnAdd and OnRemove events. It
is used by another class (Class A) as a container for the class's children.
Thus in class A's constructor, I instantiate an instance of the collection
class and register event handlers for the two events. For a give instance
of Class A, when is the appropriate time to release these handlers prior to
the instances destruction?

--Ken Baltrinic
 
N

Nicholas Paldino [.NET/C# MVP]

Kenneth,

You don't have to worry about this. When you create the delegates to
attach to your event handler, you are storing a reference to an instance of
a class (the delegate) which holds an instance to another class (the
callee). When you set the last reference to your object to null, the
references it holds do not count towards keeping another instance alive.
Because of that, you don't have to do anything.

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top