Using or Using ?

The title of this post is confusing enough! Really ? nevertheless to say, you are smart enough to guess on what we are going to talk about. One of the frequent question that comes in the .NET interview is “What is the difference between Using Directives and Using Statement ?” Sometimes the interviewers puts some extra trap by asking what are places we can use “using” ?

The first and quick answer you can thick of is all about “using directive” – which is used to define or allow the use of types as namespace.  For an instance following code block showing some namespaces inclusion with “using” directives.

using System.IO;
using System.Text;

Wait! we are not done here !! There is something called “aliasing directive” and you can define it as shown in the following code and this could be an intermediate part of the actual question!

using mynamespace = myproject.module;

Now, coming back “Using Statement” block, which defines the scope of an object, outside of which an object will be disposed.  Consider the following example,

using(ColorImageFrame imageFrame = e.OpenColorImageFrame())
{
// your code here
}

Here, the imageFrame object is declared in a using block. This means that when the execution completes the block of code the imageFrame object is no longer required and can be destroyed. This is extremely important to manage the memory efficiently.
Hope this will clear your doubts and having better understanding on using directives and using statements.

Abhijit Jana

Abhijit runs the Daily .NET Tips. He started this site with a vision to have a single knowledge base of .NET tips and tricks and share post that can quickly help any developers . He is a Former Microsoft ASP.NET MVP, CodeProject MVP, Mentor, Speaker, Author, Technology Evangelist and presently working as a .NET Consultant. He blogs at http://abhijitjana.net , you can follow him @AbhijitJana . He is the author of book Kinect for Windows SDK Programming Guide.

3 Comments to “Using or Using ?”

  1. I believe one of the biggest benefits of using the using statement is the automatic running of the Dispose() method on the object at the end of the “}”? You did elude to this when you said “can be destroyed.” I believe it can, and moreover, it will be via the Dispose().

    Have a good weekend and good catch on the ambiguities on using the using! I especially like how you caught the aliasing! Good job!

  2. Phenry,

    Yes, Disposable is an implementation of Try/Finally. Where in finally it calls Dispose on the object.

    Destroyed is in the sense, your dispose can clear unmanaged memory(which is the most important implementation of Dispose), clear out runtime window handle / file handle etc. So using would not require you to call Dispose anyway.

    I had similar feeling though when reading the article, and I though in replying the same. But for further reading, you can check my post on Internals :

    http://www.abhisheksur.com/2011/03/internals-to-net.html

  3. Omprakash

    Hi Abhijit, I have understood the difference between using directive and using block which is very clear. I want to know what is difference of using namespace & using namespace with aliasing, which is better and why? Thanks, Omprakash.

Comments are closed.