I recently discovered the "using" C# keyword and the neat and tidy way it
guarantees cleanup of resources. However, how far should I take this? I
recall some discussion from Microsoft suggesting that, in most cases, a
class's Dispose method should not be explicitly called, as it may introduce
unnecessary overhead.
For example, would this extreme be considered a best practice?
using (SqlConnection connection = new SqlConnection)
{
using (SqlCommand cmd = new SqlCommand)
{
using (SqlDataReader dr = cmd.ExecuteReader())
{
... do stuff here ...
}
}
}
or is this sufficient?
using (SqlConnection connection = new SqlConnection)
{
SqlCommand cmd ...
SqlDataReader dr ...
}
or something in-between?
Regards,
Michael Carr