Why call Connection.Close() in using block?

C

csharper

I see some people explicitly call the Close method of the
SqlConnection object in a using block. See below:

using (SqlConnection connection = new SqlConnection())
{
connection.ConnectionString = myConnectionString;
connection.Open();
// Do something;
connection.Close(); // <-- Is this necessary?
}

I don't quite understand the explicit call of connection.Close(). I
am sure it is not necessary, but does it do anything in particular?

Thank you.
 
G

Gene Wirchenko

I see some people explicitly call the Close method of the
SqlConnection object in a using block. See below:

using (SqlConnection connection = new SqlConnection())
{
connection.ConnectionString = myConnectionString;
connection.Open();
// Do something;
connection.Close(); // <-- Is this necessary?
}

I don't quite understand the explicit call of connection.Close(). I
am sure it is not necessary, but does it do anything in particular?

From the general point of view, I prefer to handle things. It
would make it clear to me that I was thinking of being done with the
connection. (This could help in debugging.) It is also very clear
where the connection is no longer needed, much more so than some "}"
ending a block. (Think blocking error.)

And then there is Big Steel's reason. I do not know enough to
say that he is right or wrong, but I have run into can-not-happen
situations before, too. I agree with his caution, and even if he is
mistaken, the extra statement does not cost much. If he is right, not
having the statement might be very costly.

Sincerely,

Gene Wirchenko
 
G

Gene Wirchenko

Then you should not be bothering with C#. Just write your code in MSIL.
C# handles all sorts of things for you, just like any other high-level
language. It is silly to eschew various language features on the basis

No kidding.
that you "prefer to handle things". Handling things on your behalf is
the _whole point_ of using a high-level language.

Nothing about levels of abstraction?
For those who know the language, a "using" statement is actually _more_
clear than an explicit try/finally block. It has a specific semantic
value, one that try/finally does not alone have.

And it ends at a difficult to see point: a brace. Is this the
brace for the using or is it the next one? I do indent, but misreads
can happen.
One should try to avoid using poorly written libraries (e.g. those that
fail to follow the .NET conventions regarding Close() and Dispose()).

One should handle the situation either way. "Well, the
documentation says it should work!" is often no taken well by the end
user.

[snip]
The bottom line: it pays to read the documentation. That is almost

The pay is not as good as it should be.
always where you'll find answers to questions like "do I need to call
Close() in addition to or instead of Dispose()?"

I really would like it if documentation were that good, but all
too often, it is incomplete. Then, there are the errors.

Once^WOften bitten, often careful.

Sincerely,

Gene Wirchenko
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top