Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The last entity to "use" an object which implements <code>IDisposable</code>, either by accessing it directly or by passing it to some other entity for <em>known-duration temporary use</em>, should call <code>Dispose</code> on it. If <code>Moe</code> is passed an <code>IDisposable</code> 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 <code>Dispose</code> itself. Indeed, it the other entity might have use for the <code>IDisposable</code> after Moe is done with it, then Moe must <em>not</em> call <code>Dispose</code> itself. If <code>Moe</code> is passed a reference of either interface or base-class type, there's no need for the interface or base class to implement <code>IDisposable</code>; even though a passed-in instance might be of a derived type which implements <code>IDisposable</code>, that's not Moe's concern. The entity which created that instance should know that it's a type which implements <code>IDisposable</code> and deal with it.</p> <p>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 <code>IDisposable</code>, 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 <code>IDisposable</code>. Because the non-generic <code>IEnumerator</code> interface did not follow that pattern, code which uses the non-generic <code>IEnumerable</code> interface is required to check whether each object returned by <code>GetEnumerator()</code> implenments <code>IDisposable</code> and call <code>Dispose</code> on it if so; this is less convenient and slower than would be calling <code>IDisposable</code> unconditionally had it been inherited by <code>IEnumerator</code> [even if only 99.9% of <code>IEnumerator</code> implementations would have a do-nothing <code>Dispose</code> method, calling a do-nothing method which an interface is known to support is faster than checking whether an interface supports a method]. <em>Note that having the factory method's return type implement or inherit <code>IDisposable</code> 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</em>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload