NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

using

The using keyword has two uses:

using [alias = ]class_or_namespace;

where:

alias
A user-defined symbol that you want to represent a namespace. You will then be able to use alias to represent the namespace name.
class_or_namespace
The namespace name that you want to either use or alias, or the class name that you want to alias.

Remarks

You would create a using alias to make it easier to qualify an identifier to a namespace or class.

You would create a using directive to use the types in a namespace without having to specify the namespace. A using directive does not give you access to any namespaces that may be nested in the namespace you specify.

Namespace come in two categories: user-defined and system-defined. User-defined namespaces are namespaces defined in your code. See the documentation for the base class library for a list of the system-defined namespaces.

Example

The following sample shows how to define and use a using alias for a namespace:

using MyAlias = MyCompany.Proj.Nested ;   // define an alias to represent a namespace

namespace MyCompany.Proj {
   public class MyClass {
      public static void DoNothing() {
      }
   }
   namespace Nested {   // a nested namespace
      public class ClassInNestedNameSpace {
         public static void SayHello() {
            System.Console.WriteLine("Hello");
         }
      }
   }

}

public class UnNestedClass {
   public static void Main() {
      MyAlias.ClassInNestedNameSpace.SayHello();   // using alias
   }
}

Output

Hello

Example

The following sample shows how to define a using directive and a using alias for a class:

using System;   // using directive
using AliasToMyClass = NameSpace1.MyClass;   // using alias for a class

namespace NameSpace1 {
   public class MyClass {
      public override string ToString() {
         return "You are in NameSpace1.MyClass";
      }
   }
}

namespace NameSpace2 {
   class MyClass {
   };
}

namespace NameSpace3 {
   using NameSpace1;   // using directive
   using NameSpace2;   // using directive

   class Test {
      public static void Main() {
         AliasToMyClass somevar = new AliasToMyClass();
         Console.WriteLine(somevar);
      }
   }
}

Output

You are in NameSpace1.MyClass

See Also

C# Keywords | Grammar