Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are different valid uses for <code>IDisposable</code>. A simple example is holding an open file, which you need to be closed at certain moment, as soon as you don't need it any more. Of course, you could provide a method <code>Close</code>, but having it in <code>Dispose</code> and using pattern like <code>using (var f = new MyFile(path)) { /*process it*/ }</code> would be more exception-safe.</p> <p>A more popular example would be holding some other <code>IDisposable</code> resources, which usually means that you need to provide your own <code>Dispose</code> in order to dispose them as well.</p> <p>In general, as soon as you want to have deterministic destruction of anything, you need to implement <code>IDisposable</code>.</p> <p>The difference between my opinion and yours is that I implement <code>IDisposable</code> as soon as some resource needs <em>deterministic</em> destruction/freeing, not necessary <em>as soon as possible</em>. Relying on garbage collection is not an option in this case (contrary to your colleague's claim), because it happens at unpredictable moment of time, and actually may not happen at all!</p> <p>The fact that any resource is unmanaged under the cover really doesn't mean anything: the developer should think in terms of "when and how is it right to dispose of this object" rather than "how does it work under the cover". The underlying implementation may change with the time anyway.</p> <p>In fact, one of the main differences between C# and C++ is the absence of default deterministic destruction. The <code>IDisposable</code> comes to close the gap: you can order the deterministic destruction (although you cannot ensure the clients are calling it; the same way in C++ you cannot be sure that the clients call <code>delete</code> on the object).</p> <hr> <p>Small addition: what is actually the difference between the <em>deterministic</em> freeing the resources and freeing them <em>as soon as possible</em>? Actually, those are different (though not completely orthogonal) notions.</p> <p>If the resources are to be freed <em>deterministically</em>, this means that the client code should have a possibility to say "Now, I want this resource freed". This may be actually not the <em>earliest possible</em> moment when the resource may be freed: the object holding the resource might have got everything it needs from the resource, so potentially it could free the resource already. On the other hand, the object might choose to keep the (usually unmanaged) resource even after the object's <code>Dispose</code> ran through, cleaning it up only in finalizer (if holding the resource for too long time doesn't make any problem).</p> <p>So, for freeing the resource <em>as soon as possible</em>, strictly speaking, <code>Dispose</code> is not necessary: the object may free the resource as soon as it realizes itself that the resource is not needed any more. <code>Dispose</code> however serves as a useful hint that the object <em>itself</em> is not needed any more, so perhaps the resources <em>may be</em> freed at that point if appropriate.</p> <hr> <p>One more necessary addition: it's not only unmanaged resources that need deterministic deallocation! This seems to be one of key points of the difference in opinions among the answers to this question. One can have purely imaginative construct, which may need to be freed deterministically.</p> <p>Examples are: a right to access some shared structure (think <a href="http://msdn.microsoft.com/en-us/library/system.threading.readerwriterlockslim.aspx" rel="noreferrer">RW-lock</a>), a huge memory chunk (imagine that you are managing some of the program's memory manually), a license for using some other program (imagine that you are not allowed to run more than <em>X</em> copies of some program simultaneously), etc. Here the object to be freed is not an unmanaged resource, but a <em>right</em> to do/to use something, which is a purely inner construct to your program logic.</p> <hr> <p>Small addition: here is a small list of neat examples of [ab]using <code>IDisposable</code>: <a href="http://www.introtorx.com/Content/v1.0.10621.0/03_LifetimeManagement.html#IDisposable" rel="noreferrer">http://www.introtorx.com/Content/v1.0.10621.0/03_LifetimeManagement.html#IDisposable</a>.</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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