How can I determine if I should extend one of my interfaces with IDisposable or implement IDisposable on a class that implements my interface?
I have an interface th
The $50,000 question is whether responsibility of disposal will ever be passed along with the interface or, to put it another way, whether the last entity to use an implementing object might be something other than the entity which creates it.
The big reason that IEnumerator implements IDisposable is that implementing objects are created by objects that implement IEnumerable but are then generally used by other objects. The object that implements IEnumerable will know whether the thing it returns really needs disposing, but will have no way of knowing when the recipient is done with it. The code that calls IEnumerable will know when it's done with the returned object, but will have no way of knowing whether it needs any cleanup. The sensible thing to do is specify that the code which calls IEnumerable is required to ensure that the returned object will have Dispose called on it before it is abandoned; the Dispose method will in many cases not do anything, but unconditionally calling a do-nothing method which is guaranteed to exist is cheaper than checking for the existence of a method that does not exist.
If the nature of your interface type is such that the the questions of whether an implementing object needs cleanup and when such cleanup should occur will both be answerable by the same entity, then there's no need for the interface to inherit IDisposable. If instances will be created by one entity but last used by another, then inheriting IDisposable would be wise.