I'm going through some old C#.NET code in an ASP.NET application making sure that all SqlConnections are wrapped in using blocks.
I know that using is the same as try / finally where it disposes of the object in the finally no matter what happens in the try. If I have a method that returns a value inside the using, even though execution leaves the method when it returns, does it still call .Dispose() on my object before/during/after it's returning?
public static SqlCommand getSqlCommand(string strSql, string strConnect){
using (SqlConnection con = new SqlConnection(strConnect))
{
con.Open();
SqlCommand cmd = GetSqlCommand();
cmd.Connection = con;
cmd.CommandText = strSql;
return cmd;
}
}
Update: The accepted answer is the one I think best answers my question but note that this answer caught the stupidity of this code, that I'm returning a command that uses a disposed connection! :P