I have a class that uses a filestream. It needs to close the stream when the app shuts down, so I make the class implement IDisposable.
That class is a member of another class, which is a member of another class etc. All the way up to my main app.
Do I therefore have to implement IDisposable on all of these classes?
What if I change my file implementation in the future so that it closes the file after each write? I now have a whole set of classes that implement IDisposable for no reason.
I guess I’m uncomfortable with crowbarring IDisposable semantics into classes that have no need for them other than some slight implementation detail way down the chain. Are there any ways around this?
In general if your type contains a member that implements
IDisposablethe type should also implementIDiposableas well. It’s the easiest way to enforce theIDisposablepattern.The one exception I use is if my types contract contains a method which 1) must be called and 2) signals the end of use for the
IDisposableresource. In that case I feel comfortable not implementingIDisposableand instead using that method to callDispose