Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the using statement be replaced by curly braces?

I use the using statement for SqlConnection. It's is good for performance because forces calling Dispose() that simply releases the connection to the pool sooner.

However, I realized that object created in using cannot be redefined. I cannot do like this:

   using (SqlConnection connection = new SqlConnection(connectionString))
   {
       connection.Open();
       //...
       connection = new SqlConnection(connectionString2);
       //...
       connection = new SqlConnection(connectionString3);
   }

I was wondering if I can replace using, and do something like this:

 {
       SqlConnection connection = new SqlConnection(connectionString);

       connection.Open();
       //...
       connection = new SqlConnection(connectionString2);
       //...
       connection = new SqlConnection(connectionString3);
 }

The SqlConnection will not be accesible after last } brace. Will be the Dispose() called immediatly when object goes out of scope?

like image 342
nan Avatar asked Sep 03 '10 12:09

nan


2 Answers

No, things won't get automatically cleaned up in your second example (in fact, with the code you have, you'll leave several connections hanging open).

Not only that, but you lose the automatic cleanup in case of Exceptions being thrown inside the using block. Remember that a using block decomposes into:

SqlConnection connection = new SqlConnection(connectionString);
try
{
    connection.Open();
    // Do work
}
finally
{
    connection.Dispose();
}

If you're really using different connections and each connection is Disposed of at the end of the block, I would use several using blocks:

using(SqlConnection connection = new SqlConnection(connectionString))
{
    connection.Open();
    // Do Work
}

// First connection is disposed

using(SqlConnection connection = new SqlConnection(connectionString2))
{
    // Do More Work
}

// Second connection is disposed

using(SqlConnection connection = new SqlConnection(connectionString3))
{
    // Do More Work
}

// Last connection is dipsosed
like image 168
Justin Niessner Avatar answered Sep 27 '22 16:09

Justin Niessner


The using statement is syntactic sugar that calls Dispose on the objects initialized within the (), so you can't simply replace it as you have in your example.

You will note that the only objects that you can use within a using statement are those that implement IDisposable, which ensures that Dispose can be called.

As this article explains, the compiler will transform this:

using (MyResource myRes = new MyResource())
{
    myRes.DoSomething();

}

To this:

MyResource myRes= new MyResource();
try
{
    myRes.DoSomething();
}
finally
{
    if (myRes!= null)
        ((IDisposable)myRes).Dispose();
}

So, unless you duplicate this structure, you won't get the same behavior.

In addition - reusing a variable the way you are in your example is bad practice. Someone who reads the code may think they are looking at connection 1 when they are in fact looking at 2 or 3. Could end up very confusing and cause all kinds of problems down the road.

like image 35
Oded Avatar answered Sep 27 '22 18:09

Oded