Via System.Diagnostics.CodeAnalysis.SuppressMessageAttribute, yep.
Use using instead of manually writing try-finally, Close, Dispose, etc., code.
SqlConnection connection;
SqlCommand command;
SqlDataReader reader;
try
{
connection = new SqlConnection(...);
command = new SqlCommand(...);
reader = command.ExecuteReader(...);
// code
}
finally
{
if (reader != null)
{
if (!reader.IsClosed())
{
reader.Close();
}
reader.Dispose();
}
if (command != null)
{
command.Dispose();
}
if (connection != null)
{
if (connection.State != ConnectionState.Closed)
{
connection.Close();
}
connection.Dispose();
}
}
vs.
using (SqlConnection connection = new SqlConnection(...))
using (SqlCommand command = new SqlCommand(...))
using (SqlDataReader reader = command.ExecuteDataReader(...))
{
// code
}
The first chunk of code looks like code written by someone used to writing C++ without auto_ptr.