By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,680 Members | 1,666 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,680 IT Pros & Developers. It's quick & easy.

2.0: SIMPLE QUESTION: "using (...)"

P: n/a
Hello,
(Sorry for my English...)
I am learning C# and I have a question:
is it good practice to use "using", for example:

using (SqlConnection connection = new SqlConnection(ConnectionString))
{
connection.Open();
...
connection.Close();
}

It could be also written:

SqlConnection connection;
try
{
connection = new SqlConnection(ConnectionString);
connection.Open();
...
}
catch (Exception ex)
{
...
}
finally
{
connection.Close();
}

What are advanatges and disadvantages of each of these methods?
Thank you very much.
/RAM/
Jun 14 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
R.A.M. <r_********@poczta.onet.pl> wrote:
(Sorry for my English...)
I am learning C# and I have a question:
is it good practice to use "using", for example:


Yes, "using" is good practice, just because it makes it easier to do
the right thing than to manually write the try/finally block. I
personally use it even when I want to handle exceptions explicitly -
I'd write:

try
{
using (SqlConnection connection ...)
{
...
}
}
catch (Exception e)
{
...
}

Or:

using (SqlConnection connection ...)
{
try
{
...
}
catch (Exception e)
{
...
}
}

However, "catch" blocks should usually be pretty rare. try/finally (or
"using") should be much more common.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 14 '06 #2

P: n/a
Hello R.A.M.,

"using" just simplify your work, because sometime you can forget to free
you resources

R> Hello,
R> (Sorry for my English...)
R> I am learning C# and I have a question:
R> is it good practice to use "using", for example:
R> using (SqlConnection connection = new
R> SqlConnection(ConnectionString))
R> {
R> connection.Open();
R> ...
R> connection.Close();
R> }
R> It could be also written:
R>
R> SqlConnection connection;
R> try
R> {
R> connection = new SqlConnection(ConnectionString);
R> connection.Open();
R> ...
R> }
R> catch (Exception ex)
R> {
R> ...
R> }
R> finally
R> {
R> connection.Close();
R> }
R> What are advanatges and disadvantages of each of these methods?
R> Thank you very much.
R> /RAM/
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Jun 14 '06 #3

P: n/a
Yes, it's good practice.

It's so good that you don't even have to call the Close method on the
connection:

using (SqlConnection connection = new SqlConnection(ConnectionString))
{
connection.Open();
...
}

The using structure will call the Dispose method of the connection, and
the Dispose method calls the Close method, so you can safely omit it.
R.A.M. wrote:
Hello,
(Sorry for my English...)
I am learning C# and I have a question:
is it good practice to use "using", for example:

using (SqlConnection connection = new SqlConnection(ConnectionString))
{
connection.Open();
...
connection.Close();
}

It could be also written:

SqlConnection connection;
try
{
connection = new SqlConnection(ConnectionString);
connection.Open();
...
}
catch (Exception ex)
{
...
}
finally
{
connection.Close();
}

What are advanatges and disadvantages of each of these methods?
Thank you very much.
/RAM/

Jun 14 '06 #4

P: n/a
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...

Yes, "using" is good practice, just because it makes it easier to do
the right thing than to manually write the try/finally block.


I also like how it signals that you're acquiring a resource that has time
value. That's executable documentation that it should be disposed of
quickly.
Jun 14 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.