PC Review


Reply
Thread Tools Rate Thread

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

 
 
R.A.M.
Guest
Posts: n/a
 
      21st Jun 2006
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/
 
Reply With Quote
 
 
 
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      21st Jun 2006
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]
- (E-Mail Removed)

"R.A.M." <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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/



 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      21st Jun 2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Beginner's question - Catch a "HyperLink" click event Larry Smith Microsoft ASP .NET 5 6th Feb 2009 12:44 PM
Re: Beginner's question - Catch a "HyperLink" click event Larry Smith Microsoft ASP .NET 0 5th Feb 2009 11:27 PM
Beginner getting "Prepared statement...expects parameter...not supplied" error Greg Microsoft ADO .NET 4 15th Aug 2006 02:07 AM
Access Beginner => "sum operation cannot take a varchar data type as an argument" markx Microsoft Access Getting Started 5 25th Aug 2005 08:30 AM
BEGINNER: "System exception: access violation" IN NEED Microsoft Frontpage 1 23rd Oct 2003 12:00 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:07 AM.