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

beginner's question: usage of "using (...)"

P: n/a
Hello,
I have started larning C# and I have a question concerning "using
(...)" keyword.
For example:

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

I can write it also this way:

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

But it won't dispose 'connection'.
Which solution is better? What are adventages and disadventages?
Thank you very much
/RAM/
Jun 21 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
RAM,

When you have the using statement, the Dispose method on the IDisposable
implementation is called when the scope of the using statement is exited.

With the SqlConnection, calling Dispose is the same as calling Close, so
you don't have to explicitly call it. You can do this:

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

// Other stuff.
}

What this really compiles to is this:

SqlConnection connection = new SqlConnection(ConnectionString);

try
{
connection.Open();

// Other stuff.
}
finally
{
if (connection != null)
{
// Call Dispose.
((IDisposable) connection).Dispose();
}
}

So, if you have the need for exception handling, then you can use the
above construct and just insert the catch statements appropriately.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"R.A.M." <r_********@poczta.onet.pl> wrote in message
news:16********************************@4ax.com...
Hello,
I have started larning C# and I have a question concerning "using
(...)" keyword.
For example:

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

I can write it also this way:

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

But it won't dispose 'connection'.
Which solution is better? What are adventages and disadventages?
Thank you very much
/RAM/

Jun 21 '06 #2

P: n/a
The main advantage of "using" is that it is easier to get it right, and
hence correctly dispose of objects. It saves you from having to do a
try / finally (which as your example shows, can be a mouthful), and
also deals with the situation where it initialises to null.

You can also have multiple same-level "using"s, and it will work
happily without having to worry about your "finally" barfing and so not
disposing all the objects - e.g.

using(Form f = new Form())
using(Button b = new Button()) {

// both f & b will be disposed as we exit this brace
}

That is obviously a lot tidier than the equivalent in try / finally
notation (remembering to dispose f if the b ctor throws, etc)

Finally, because of the above points, it saves you from excessive
indentation, which makes it easier to read (and thus support) the code.

"using" is your friend. Get to know it well ;-p

Marc

Jun 21 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.