A correctly written program that creates an instance of a type that implements IDisposable , and which, as you know, is not able to adequately clean after itself after a failure, must ensure that Dispose is called in this instance before abandoning it. Any program that cannot call Dispose on a type that is not exactly known does not work without it.
Although it would be nice if automatic finalization could take care of everything, this is a rather muddy cleanup mechanism. It does not give any guarantees regarding the sequence, thread context, timeliness or confidence in completion (when using deterministic cleaning, you can make sure enough that the program will not appear normal if the cleaning fails; when using finalization, the program may, as a rule, not even appear trying to clear an object).
Microsoft may IDisposable assume that every IDisposable class should be able to properly clean up after itself if it is left, but that is simply impractical. In many cases, for a class trying to clear after itself, leaving it will add enormous complexity and simply turn a broken program, which will have obvious problems that are easy to track down, into a broken program, which usually works except when the synchronization of the finalizer thread is relatively some other thread makes things fail in some unexpected and irreproducible way.
There are some types that, despite the implementation of IDisposable , are unconditionally safe for rejection, and there are some others that can be safely left under certain circumstances. It’s great to refuse such types in situations where it would be difficult to remove them (for example, because links are stored by several objects that are manipulated by various threads, and there is no good way that any particular object can know it when it is held by the last survivor link), provided that one of the documents gives reason to believe that such an action is safe and appropriate. However, this behavior is unacceptable in those cases when IDisposable objects of an unknown line are accepted.
source share