Although objects that implement interfaces generally promise capabilities or characteristics that are not common to objects without these interfaces, and although IDisposable is an interface, the fact that an object implements IDisposable does not promise any ability or characteristics will not be present in objects that they don’t do it. Instead, the fact that an object implements IDisposable implies that it lacks a characteristic characteristic of objects that do not implement it: the ability of anyone who acquires or contains a link refuses it, regardless of whether the object can or should to be cleaned first.
If code that uses a particular type of interface is usually not the last link maintained, then there is no need for an interface to implement IDisposable . Even if some implementations cannot be safely left behind, it does not matter to any users of the instance other than the last containing the link. If this user usually knows more about a particular type than the interface implies, the user will find out if the object needs to be cleaned, if the interface points to it.
On the other hand, if the last user of the object knows nothing about this at all, except for the fact that he implements an interface, then this interface should inherit IDisposable even (perhaps especially!), If only a small part of the implementations require cleaning. Consider the case of IEnumerable compared to IEnumerable<T> . Any code that calls IEnumerable<T>.GetEnumerator() will get what is likely to be the only link anywhere in the universe for an object that implements IDisposable . Therefore, this code takes responsibility for ensuring that Dispose called to this link. Any code that calls IEnumerable<T>.GetEnumerator() and does not call Dispose on the returned value, and does not pass it to another code that promises does this, is violated.
The type returned by non-generic IEnumerable.GetEnumerator does not implement IDisposable . This suggests that, in his opinion, the code that calls IEnumerable.GetEnumerator is not responsible for its use. Unfortunately, this implication is incorrect. The code that calls IEnumerable.GetEnumerator is responsible for ensuring that if the returned instance returns IDisposable objects, then its Dispose method must be called. Code that does not support this responsibility is no less corrupted than code that cannot order the return from IEnumerable<T>.GetEnumerator . A return type failure of IEnumerable.GetEnumerator (i.e. IEnumerator ) to implement IDisposable does not eliminate the responsibility of the caller to clean up the returned object. It just makes that responsibility more onerous and increases the likelihood that the code will not be able to do this.
supercat
source share