The using keyword has two uses:
using [alias = ]class_or_namespace;
where:
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.
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 } }
Hello
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); } } }
You are in NameSpace1.MyClass
C# Keywords | Grammar