Using is one of those dual use keywords in C#. Everybody uses using at the top of your code file so that you don’t have to type all of those long namespaces. The other use is for taking control over the closing and disposing of objects that support the IDisposable interface. Sometimes a little using goes a long way. One IDisposable class that is a good candidate for using is SqlConnection. Without using code to access a database, you would need to use a try-finally block to make sure you free up the resource.
SqlConnection conn = new SqlConnection(ConnectionString);
try
{
….
}
finally
{
conn.Close();
}
Using using the code becomes much cleaner and logical.
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
….
}
Next time you consider using resources, consider using using and clean your code up.