PC Review


Reply
Thread Tools Rate Thread

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

 
 
R.A.M.
Guest
Posts: n/a
 
      14th Jun 2006
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/
 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      14th Jun 2006
R.A.M. <(E-Mail Removed)> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Michael Nemtsev
Guest
Posts: n/a
 
      14th Jun 2006
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


 
Reply With Quote
 
=?ISO-8859-1?Q?G=F6ran_Andersson?=
Guest
Posts: n/a
 
      14th Jun 2006
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/

 
Reply With Quote
 
Mark Wilden
Guest
Posts: n/a
 
      14th Jun 2006
"Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> 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.


 
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
Simple Question about "Have replies sent to" feature in Outlook 20 Quin Microsoft Outlook Discussion 1 24th Apr 2009 02:51 AM
simple question "disable command buttons" eighthman11 Microsoft Excel Programming 0 10th Oct 2006 09:57 PM
"Simple ?" beginners web form design question Ray Watson Microsoft Dot NET 1 8th Jan 2004 03:09 PM
A question about "Safe, Simple Multithreading in Windows Forms, Part 1" on MSDN Jeff Yang Microsoft C# .NET 1 5th Dec 2003 02:41 PM
Question about how IE parses simple HTTP "put" statements Robert Gordon Windows XP Internet Explorer 0 22nd Jul 2003 06:22 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:51 PM.