Note that there are some explanatory texts on larger screens.

plurals
  1. POShould "Dispose" only be used for types containing unmanaged resources?
    text
    copied!<p>I was having a discussion with a colleague recently about the value of <code>Dispose</code> and types that implement <code>IDisposable</code>.</p> <p>I think there is value in implementing <code>IDisposable</code> for types that should clean up as soon as possible, <strong>even if there are no unmanaged resources to clean up</strong>.</p> <p>My colleague thinks differently; implementing <code>IDisposable</code> if you don't have any unmanaged resources isn't necessary as your type will eventually be garbage collected.</p> <p>My argument was that if you had an ADO.NET connection that you wanted to close as soon as possible, then implementing <code>IDisposable</code> and <code>using new MyThingWithAConnection()</code> would make sense. My colleage replied that, under the covers, an ADO.NET connection is a <strong>unmanaged resource</strong>. My reply to his reply was that <strong>everything ultimately is an unmanaged resource</strong>.</p> <p>I am aware of the <a href="http://msdn.microsoft.com/en-us/library/b1yfkh5e%28v=vs.71%29.aspx">recommended disposable pattern</a> where you <strong>free managed and unmanaged resources if <code>Dispose</code> is called</strong> but <strong>only free unmanaged resources if called via the finalizer/destructor</strong> (and blogged a while ago about how to <a href="http://stevedunns.blogspot.co.uk/2009/03/idisposable-alerts.html">alert consumers of improper use of your IDisposable types</a>)</p> <p>So, my question is, if you've got a type that doesn't contain unmanaged resources, is it worth implementing <code>IDisposable</code>?</p>
 

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