The last entity to "use" an object which implements IDisposable
, either by accessing it directly or by passing it to some other entity for known-duration temporary use, should call Dispose
on it. If Moe
is passed an IDisposable
from some other entity which will inherently know when Moe is done with it, then Moe can generally expect that other entity to take care of it and need not call Dispose
itself. Indeed, it the other entity might have use for the IDisposable
after Moe is done with it, then Moe must not call Dispose
itself. If Moe
is passed a reference of either interface or base-class type, there's no need for the interface or base class to implement IDisposable
; even though a passed-in instance might be of a derived type which implements IDisposable
, that's not Moe's concern. The entity which created that instance should know that it's a type which implements IDisposable
and deal with it.
The place things get tricky is with factory methods. If there's any realistic possibility that a factory method might return an object which implements IDisposable
, and that the caller of that method would be the only thing that would know when the object is no longer needed, then the return type of that method should itself implement IDisposable
. Because the non-generic IEnumerator
interface did not follow that pattern, code which uses the non-generic IEnumerable
interface is required to check whether each object returned by GetEnumerator()
implenments IDisposable
and call Dispose
on it if so; this is less convenient and slower than would be calling IDisposable
unconditionally had it been inherited by IEnumerator
[even if only 99.9% of IEnumerator
implementations would have a do-nothing Dispose
method, calling a do-nothing method which an interface is known to support is faster than checking whether an interface supports a method]. Note that having the factory method's return type implement or inherit IDisposable
wouldn't add any responsiblity to the caller--it would merely make it easier for callers to carry out the responsibility they will have regardless.