Possible Duplicate:
If I return a value inside a using block in a method, does the using dispose of the object before the return?
I have this code (simplyfied):
bool method1()
{
using (OleDbConnection con = new OleDbConnection(connString))
{
bool b = false;
try
{
con.Open();
b = true;
}
catch (Exception)
{
b = false;
}
finally
{
con.Close();
return b;
}
}
}
I return before the closing curly bracket of the "using" statment. Does my object "con" gets Disposed anyway? Is is better to use the following code?:
bool method1()
{
bool b = false;
using (OleDbConnection con = new OleDbConnection(connString))
{
try
{
con.Open();
b = true;
}
catch (Exception)
{
b = false;
}
finally
{
con.Close();
}
}
return b;
}