Geeks With Blogs

News



Microsoft Store

Support This Site


AddThis Social Bookmark Button

Locations of visitors to this page

Subscribers to this feed

TwitterCounter for @sdorman

Creative Commons License


Scott Dorman Microsoft MVP, Software Architect, Developer, Author

"Deuces you say", there is no difference...right? Wrong!

In the C# language, using is a keyword. Unfortunately, this is one of those times that a single keyword has multiple uses:

  • As a preprocessor directive used to create an alias for a namespace or to import types defined in other namespaces.
  • As a statement used to define a scope at the end of which an object will be disposed.

It's unfortunate that Microsoft chose to attach multiple meanings to this keyword as it tends to cause a lot of confusion for newer developers. I think it's time to try clear up some of that confusion. (This  issue only affects C#. The other .NET languages do use a different keyword for the directive and the statement forms.)

The Using Directive

The using Directive (C#) actually has two uses:

  1. To allow the use of types in a namespace without needing to fully qualify that type.
  2. To create an alias for a namespace or a type.

In both of these scenarios, the using directive must appear at the beginning of the file and it's scope is limited to the file in which it appears. If you are referencing a namespace in a different assembly, that assembly must be included in your project references. Also, there is no harm in having "unused" using directives. These are namespaces or types that are referenced by a using directive that aren't actually needed by the code. (If you're using Visual Studio 2008, there is a new context menu that allows you to clean these up.)

Here are some examples of the using directive. These examples are taken from the MSDN documentation, so there is nothing "magic" about them.

Example 1: Define and use a using alias for a namespace.

   1: namespace PC
   2: {
   3:     // Define an alias for the nested namespace.
   4:     using Project = PC.MyCompany.Project;
   5:     class A 
   6:     {
   7:         void M()
   8:         {
   9:             // Use the alias
  10:             Project.MyClass mc = new Project.MyClass();
  11:         }
  12:     }
  13:     namespace MyCompany
  14:     {
  15:         namespace Project
  16:         {
  17:             public class MyClass{}
  18:         }
  19:     }
  20: }

Example 2: Define a using directive and a using alias for a class.

   1: // Using directive.
   2: using System;   
   3:  
   4: // Using alias for a class.
   5: using AliasToMyClass = NameSpace1.MyClass;   
   6:  
   7: namespace NameSpace1 
   8: {
   9:     public class MyClass 
  10:     {
  11:         public override string ToString() 
  12:         {
  13:             return "You are in NameSpace1.MyClass";
  14:         }
  15:     }
  16: }
  17:  
  18: namespace NameSpace2 
  19: {
  20:     class MyClass 
  21:     {
  22:     }
  23: }
  24:  
  25: namespace NameSpace3 
  26: {
  27:     // Using directive:
  28:     using NameSpace1;
  29:     // Using directive:
  30:     using NameSpace2;   
  31:  
  32:     class MainClass
  33:     {
  34:         static void Main() 
  35:         {
  36:             AliasToMyClass somevar = new AliasToMyClass();
  37:             Console.WriteLine(somevar);
  38:         }
  39:     }
  40: }

The Using Statement

The using Statement (C#) allows programmers to specify when an object (or objects) that use resources should release them. The object provided to the using statement must implement the IDisposable Interface (System). A using statement is exited when the end of the statement is reached or an exception is thrown and control leaves the statement block early. This is a compile time "translation" that occurs where the compiler actually translates this to a try/finally block.

The using statement can be declared in different ways:

   1: // Object declared in the using statement
   2: using (Font font1 = new Font("Arial", 10.0f))
   3: {
   4: }
   5:  
   6: // Object declared outside (before) the using statement
   7: Font font2 = new Font("Arial", 10.0f);
   8: using (font2)
   9: {
  10: }
  11:  
  12: // Multiple objects (must be declared inside the using statement, and must
  13: // all be of the same type)
  14: using (Font font3 = new Font("Arial", 10.0f), font4 = new Font("Arial", 10.0f))
  15: {
  16: }
Posted on Tuesday, August 28, 2007 6:59 PM .NET (C#) | Back to top

Copyright © Scott Dorman | Powered by: GeeksWithBlogs.net