Andrew said:
Thank you for this article. I hadn't found that one yet (and might
not have found it). I hope you see my response, or someone else does,
I want to make sure that my solution is sound.
After thinking about it my derived classes do not have anything
objects in them that they must free.
This is only a valid conclusion if you already know all derived classes that
will ever be. Keep in mind that you can't enforce this -- if a class is
accessible and unsealed anyone can derive from it. Generally it saves more
time assuming that everybody will (possibly including a future you) than to
assume you have complete control and fix the avoidable problems afterwards.
Indeed, the only objects giving me difficulty were the objects contained
in the base class. So, I've implemented IDisposable on the base class
according to the link I first posted.
This is dangerous because any derived class that does need to introduce its
own disposable members is going to have a hard time doing this correctly.
It's still possible using method hiding and re-implementing IDisposable, but
it's not intuitive, nor is it safe (derived classes cannot be forced to call
their parent's Dispose() methods, as they should).
Basically, the only place that the Dispose() function was needed as in
the base class. However, I'm wondering if I should still implement
according to the link you've provided because, although the derived
objects don't need to dispose of anything, should I have a virtual
protected Dispose()/override Dispose() in this base<- derived
relationship?
Yes, because the overhead is negligible compared to the horrible mess you'll
have on your hands if this turns out to be the wrong decision.
This is a standard pattern you should follow because it makes everyone's
life easier. You should only forego a virtual Dispose() if your class is
sealed and thus has no derived classes to worry about in the first place.