User kokos answered the wonderful Hidden Features of C# question by mentioning the using
keyword. Can you elaborate on that? What are the uses of
Things like this:
using (var conn = new SqlConnection("connection string"))
{
conn.Open();
// Execute SQL statement here on the connection you created
}
This SqlConnection
will be closed without needing to explicitly call the .Close()
function, and this will happen even if an exception is thrown, without the need for a try
/catch
/finally
.