Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# using keyword,Proper use of it

Tags:

c#

.net

asp.net

clr

which one is better from following options

Is one using statement is enough?

Option 1:

using(SqlConnection con = new SqlConnection(constring))
{
   using(SqlCommand cmd = new SqlCommand())
   {
       .........................
       .........................
       .........................
   }
}

Option 2:

using(SqlConnection con = new SqlConnection(constring))
{
   SqlCommand cmd = new SqlCommand();
   .........................
   .........................
   .........................
}
like image 963
Ali Avatar asked Mar 02 '12 17:03

Ali


1 Answers

It's generally simplest to follow the rule, "If the type implements IDisposable then use a using construct." So I'd go with some form of option 1.

like image 93
Kirk Woll Avatar answered Oct 05 '22 23:10

Kirk Woll