Question

If I implement a object with IDisposable, should all objects that own that object implement it as well, even if they have no other resources to release?

Was it helpful?

Solution

Yes. You need to dispose of them, in order to have your member variables get disposed correctly.

Any time you encapsulate an IDisposable class, you should make your class IDisposable. In your Dispose method, you should dispose your encapsulated resources. Basically, treat them the same way you would treat a native resource.

OTHER TIPS

If you want deterministic disposal, ultimately some client needs to call Dispose or wrap the calls in a "using" block. To trickle down to your object, that might require that the owner implement IDisposable as well.

You shouldn't rely on the garbage collector to free any time-dependent resources.

Yes, an owning class should implement IDisposable but it does not need (should not have) a Finalizer (destructor).

nop, they just need to use that class with a "using" statement to make sure they dispose that object properly, but those objects themselves don't need to implement IDisplosable

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top