What’s the Purpose of the “using” Statement?

I recently was asked by one of my clients “What’s the Purpose of the “using statements”? ” The developer that asked me this title is Senior Engineer. He went on to state “Looking at the <SOME OTHER> project, I see they didn’t use “using”“. When a senior developer actually asks me this I cringe. What I mean by “actually” is the most developers, no matter at what level, have no idea what this is for or know the “why” either.

Microsoft states that using “provides a convenient syntax that ensures the correct use of IDisposable objects”. Well next you might ask is what are IDisposable objects? They are types that need to release unmanaged resources. These resources are usually (but not always) system resources such as a file, window, or network connections and many more. While .NET (Garbage Collector) maintains and releases managed (.NET type) it does not for other resources.

If you develop a type that needs to clean up these unmanaged resources it’s very important that you implement IDisposible properly. Even more important is as a developer that uses one of these types, you must use it properly! If you don’t, you can cause memory leaks… ok they are not memory leaks you might have heard of in C++ applications (like Internet Explorer) they are “virtual memory leaks” that can  eat up all the memory on the system until it stops, not release connections to SQL Server and even worse!

When I’m speaking on this subject at one of my coding standard conference sessions, I ask the audience if anyone has a server that for some unexplained reason they have to reboot it every day or week etc. Unfortunately hands always go up, then I tell them they have a memory leak and lights start going off in their heads. I have personally seen in production projects that were many hundreds of thousands of lines of code that this was not being done correctly at all!

This post is directed towards a developer using one of these types. How do you know if a type implements IDisposible? It’s easy, just type the variable name for the object then . then D to see if the Dispose method comes up in IntelliSense like this:


If you see .Dispose call it as soon as you are done with the object, not at the end of the method. You want to release these resources sooner than later for better memory optimization. The biggest problem with this (besides you remembering to do this) is that if an exception is thrown before Dispose is called then the object might never be properly be released from memory… causing a virtual memory leak. So the safest way to do this is like this:

    StreamedImage image = new StreamedImage();

Using the finally block guarantees that Dispose will be called if an exception was thrown or not. This will prevent any memory issues. In later versions of .NET the using statement or pattern was introduced. Now, you only need to do this:

using(StreamedImage image = new StreamedImage())
//Your code goes here

That’s it! Very simple. What actually happens when you compile your code the compiler writes the try – finally block for you. The using pattern is just a way to get away with less coding. I also like the readability of the code better too.

I will soon write a post on the proper way to implement the IDisposible pattern for your types.

3 thoughts on “What’s the Purpose of the “using” Statement?

  1. Really appreciate the post, David. I sure didn’t know this either, so here are some hopefully pertinent questions:

    I and many of the folks I have encountered in my work are mostly ‘self-taught’, so our knowledge is acquired by reading forums like this, buying books, and mostly in the trenches while in ‘get her done’ mode. Is this the best place to learn? How can one avoid spending years working in C# and still not knowing these kinds of things? (hint: I started in computers with Dyna Micro 8080 assembly code).

    Where do you recommend one turn to learn the gamut of things like this, including the ‘when & why’ – not just the what for all these kinds of things that you don’t know you don’t know? I’ve looked for local tech college classes, etc. but found only very basic things for people who have little experience using computers, much less compilers.

    1. Thanks for your great comment. I see “best practices” like this not being done by every level of developer. Once place to to start is with my coding standards book (shameless plug): http://www.lulu.com/spotlight/davidmccarter. I teach at a local collage and always try to teach my students these things from the beginning. If you have any specific questions, let me know and I will be happy to write a post on them.

Comments are closed.