Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <code>using</code> statement</p> <pre><code>using(var disposableObject = new object_that_implements_IDisposable()) { ... } </code></pre> <p>is syntactic sugar for code similar to following:</p> <pre><code>var disposableObject = new object_that_implements_IDisposable() try { ... } finally { if(disposableObject != null) { ((IDisposable)your_object).Dispose(); } } </code></pre> <p>This is <strong>only</strong> applicable for classes that implement IDisposable. It is helpful for cleaning up code where you have objects that take, for example, system resources (file handles, database connections, sockets, etc.) that need to be cleaned up after you are done to free the resource for the rest of the system.</p> <p>In theory, you could leave out the <code>.Dispose()</code> call, but then you would have to wait for the Garbage Collector to free the kept resources. The GC is awesome at knowing when to free objects to reclaim their memory usage, but it has <em>no idea</em> that it needs to free objects to have them give up other system resources. Thus, these critical resources might not be given up until after the GC decides it needs the memory used by the owner. Thus, you definitely want to dispose of your objects when you are done with them (if they are disposable)!</p> <p>As to why you'd use <code>using</code> over <code>try</code>/<code>finally</code>, it is purely a coder's preference. I prefer <code>using</code> because you can cascade them:</p> <pre><code>using(var a = new class()) using(var b = new class()) using(var c = new class()) using(var d = new class()) { ... } </code></pre> <p>You'd need quite a few more lines of code to do that with <code>try</code>/<code>finally</code>.</p> <p><code>using</code> has additional advantages as well. For example, whereas calling <code>x.Dispose</code> directly might throw a <code>NullReferenceException</code> if <code>x</code> is null, <code>using(x)</code> will not.</p> <p>See also:</p> <ul> <li><a href="http://blogs.msdn.com/b/shawnfa/archive/2004/03/29/101466.aspx" rel="nofollow noreferrer">http://blogs.msdn.com/b/shawnfa/archive/2004/03/29/101466.aspx</a></li> <li><a href="https://stackoverflow.com/questions/614959/using-the-using-statment-in-c">Using the using statment in c#</a></li> <li><a href="https://stackoverflow.com/questions/212198/what-is-the-c-using-block-and-why-should-i-use-it">What is the C# Using block and why should I use it?</a></li> <li><a href="http://msdn.microsoft.com/en-us/library/yh598w02%28VS.80%29.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/yh598w02%28VS.80%29.aspx</a></li> </ul>
    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. 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