15

I have been looking at using in C# and I want to know if the following code is equivalent;

using (SqlConnection connection1 = new SqlConnection(), connection2 = new SqlConnection())
{
}

To this code;

using (SqlConnection connection1 = new SqlConnection())
using (SqlConnection connection2 = new SqlConnection())
{
}
Ben Brocka
  • 2,006
  • 4
  • 34
  • 53
Lukasz
  • 8,710
  • 12
  • 44
  • 72

6 Answers6

27

The C# Spec says,

When a resource-acquisition takes the form of a local-variable-declaration, it is possible to acquire multiple resources of a given type. A using statement of the form

    using (ResourceType r1 = e1, r2 = e2, ..., rN = eN) statement

is precisely equivalent to a sequence of nested using statements:

    using (ResourceType r1 = e1)
       using (ResourceType r2 = e2)
          ...
             using (ResourceType rN = eN)
                statement
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
7

You can of course insert some code between the first and second using, that uses connection1 before connection2 is created.

But you aren't, so as it is there is no difference. They both produce the same IL:

IL_0000:  newobj      System.Data.SqlClient.SqlConnection..ctor
IL_0005:  stloc.0     
IL_0006:  newobj      System.Data.SqlClient.SqlConnection..ctor
IL_000B:  stloc.1     
IL_000C:  leave.s     IL_0018
IL_000E:  ldloc.1     
IL_000F:  brfalse.s   IL_0017
IL_0011:  ldloc.1     
IL_0012:  callvirt    System.IDisposable.Dispose
IL_0017:  endfinally  
IL_0018:  leave.s     IL_0024
IL_001A:  ldloc.0     
IL_001B:  brfalse.s   IL_0023
IL_001D:  ldloc.0     
IL_001E:  callvirt    System.IDisposable.Dispose
IL_0023:  endfinally  
Jon Hanna
  • 110,372
  • 10
  • 146
  • 251
5

Yes, those two pieces of code are equivalent.

Edit

Just tested this with Reflector. Exact same IL is emitted for the two versions, and Reflector decompiles to the following C#:

using (new SqlConnection())
{
    using (new SqlConnection())
    {
    }
}

That is, for both versions, Dispose will be called on both instances, even if one throws an exception in the constructor.

driis
  • 161,458
  • 45
  • 265
  • 341
5

Yes, according to section 8.13 of the C# Language Specification:

When a resource-acquisition takes the form of a local-variable-declaration, it is possible to acquire multiple resources of a given type. A using statement of the form

using (ResourceType r1 = e1, r2 = e2, ..., rN = eN)

statement is precisely equivalent to a sequence of nested using statements:

using (ResourceType r1 = e1)
using (ResourceType r2 = e2)
...
using (ResourceType rN = eN)
    statement

casperOne
  • 73,706
  • 19
  • 184
  • 253
3

Yes, they are exactly the same. You can use Reflector to prove this.

    public void Method1()
    {
        using (SqlConnection connection1 = new SqlConnection())
        using (SqlConnection connection2 = new SqlConnection())
        {
        }
    }

    public void Method2()
    {
        using (SqlConnection connection1 = new SqlConnection(), connection2 = new SqlConnection())
        {
        }
    }
Bryan
  • 2,775
  • 3
  • 28
  • 40
1

In addition to the excellent mentioning of C# 8.13 of other answers, the single-clause case is going to have to be rewritten when one of the resources has to change type.

using( S r1 = new S(), r2 = new S() )
{
  ...
}

When r2 needs to be changed into a new type, this has to become

using( S r1 = new S() )
using( T r2 = new T() )
{
  ...
}

So you're better of using the latter. (Personally, I find it reads better, too)

xtofl
  • 40,723
  • 12
  • 105
  • 192