Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strike>Garbage Collection (while not directly related to IDisposable, is what cleans up unused objects) isn't that simple.</strike></p> <p>Let me re-word this a little bit. Automatically calling <code>Dispose()</code> isn't that simple. It also won't directly increase performance. More on that a little later.</p> <p>If you had the following code:</p> <pre><code>public void DoSomeWork(SqlCommand command) { SqlConnection conn = new SqlConnection(connString); conn.Open(); command.Connection = conn; // Rest of the work here } </code></pre> <p>How would the compiler know when you were done using the <code>conn</code> object? Or if you passed a reference to some other method that was holding on to it?</p> <p>Explicitly calling <code>Dispose()</code> or using a <code>using</code> block clearly states your intent and forces things to get cleaned up properly.</p> <p>Now, back to performance. Simply calling <code>Dispose()</code> on an Object doesn't guarantee any performance increase. The <code>Dispose()</code> method is used for "cleaning up" resources when you're done with an Object.</p> <p>The performance increase can come when using un-managed resources. If a managed object doesn't properly dispose of its un-managed resources, then you have a memory leak. Ugly stuff.</p> <p>Leaving the determination to call <code>Dispose()</code> up to the compiler would take away that level of clarity and make debugging memory leaks caused by un-managed resources that much more difficult.</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