Disposing Collection of IDisposable?
-
Hi folks, Having just discovered the delights of using IDisposable properly, I have a question concerning the disposal of collections of IDisposable objects. The scenario is as follows: I have a class implementing IDisposable. This class owns a collection of other objects that implement IDisposable. So, when I dispose the main object, should I dispose the contents of the array as well? Any advise, comments and/or opinions welcome. PS: Sorry for any typos I suffering mild RSI (three days of furious coding) Dr Herbie @ Autoscribe Remember, half the people out there have below average IQs.
-
Hi folks, Having just discovered the delights of using IDisposable properly, I have a question concerning the disposal of collections of IDisposable objects. The scenario is as follows: I have a class implementing IDisposable. This class owns a collection of other objects that implement IDisposable. So, when I dispose the main object, should I dispose the contents of the array as well? Any advise, comments and/or opinions welcome. PS: Sorry for any typos I suffering mild RSI (three days of furious coding) Dr Herbie @ Autoscribe Remember, half the people out there have below average IQs.
If the class "owns" the objects (e.g., private fields) then, yes, you should dispose them. If the class is merely a container for the objects (e.g., a collection like ArrayList), then no, the class user is responsible for that. Due to technical difficulties my previous signature, "I see dumb people" will be off until further notice. Too many people were thinking I was talking about them... :sigh: