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

  • Thread starter Thread starter R.A.M.
  • Start date Start date
R

R.A.M.

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/
 
R.A.M. said:
(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.
 
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
 
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.
 
Jon Skeet said:
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.
 

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

Back
Top