title: Difference between Try-Finally Dispose and Using-statement
body: <p>I have been diving into Code Analysis of Microsoft and stumbled upon something quite interesting. .NET seems to use two different types of Dispose, depending on the way it is called. Take the following two options:</p>
  
  <pre><code>public void SqlConnectionUsing()
  {
      using (SqlConnection connection = new SqlConnection())
      {
      }
  }
  
  public void SqlConnectionFinally()
  {
      SqlConnection connection = new SqlConnection();
      try
      {
      }
      finally
      {
          connection.Dispose();
      }
  }
  </code></pre>
  
  <p>Both options get translated to exactly the same thing; during compilation. The using becomes a try-finally statement with inside the finally statement a call to a Dispose-method.</p>
  
  <p>I say <strong>a</strong> dispose-method; because what kind of dispose-method depends on the way you have written your code. </p>
  
  <p>When going for the <code>using-statement</code>, a call goes to <code>callvirt instance void [mscorlib]System.IDisposable::Dispose()</code> (it is the exact IL-line).</p>
  
  <p>And manually taking the try-finally option, the dispose-statement changes to: <code>callvirt instance void [System]System.ComponentModel.Component::Dispose()</code>.</p>
  
  <p>Why is there a difference in what dispose function gets called?</p>
  
  <p>I can add the entire IL-code if required.</p>