Using the Using Statement

Rory shows us a couple of examples of how to use the C# using statement.  Thankfully VB 2005 also includes this statement so that VB.NET coders can write:

Using ts As New Data.SqlClient.SqlConnection, _
          ts2 As New Data.SqlClient.SqlConnection
      ‘Do stuff…
End Using

What I would like to see is a compiler that is intilligent enough so that if I declare an IDisposable object on the stack (ie as a method variable) it will automatically call dispose when the method exits.  This would mostly negate the need for the using statement.  Oh, wait doesn’t the Managed C++/CLI compiler do this….

Leave a comment