Always Get Better

C#: using Statement

One of my absolute favourite statements in C# is the using statement (not to be confused with the using directive, which is what we use to import libraries like System.Web into our projects).

using forces us as programmers to be honest about releasing memory to the CLR. Whenever we use an unmanaged resource like an SQL connection or file IO handler, the garbage collector will eventually eliminate any open streams or connections associated with that resource. However, “eventually” doesn’t cut it when we’re dealing with SQL connections on a production server – we need to make sure the connections are released no later than when we’re done with them.

If you come from the C++ world, you’re probably (hopefully) used to calling delete to deallocate any memory you reserved. You also know that forgetting the delete (or delete [] on arrays) results in a memory leak. You might think of Dispose() as C#’s implementation of the delete statement.

using ( SqlCommand cmd = new SqlCommand( sqlStatement, sqlConnection ) )
// Do something

using acts as a try…catch…finally block, so if your code fails your object will still be disposed. The using statement keeps everything wrapped into a neat little package so you don’t forget to keep your local variables in scope.

Like I said, this is one of my favourite features in .NET (lock { } is similarly beautiful). You can use the same construct in VB as well.

Tags: ,

Leave a Reply