Information Technology Reference
In-Depth Information
The using Alias Directive
The
using alias directive
allows you to assign an alias for either of the following:
A namespace
A type in a namespace
For example, the following code shows the use of two
using
alias directives. The first direc-
tive instructs the compiler that identifier
Syst
is an alias for namespace
System
. The second
directive says that identifier
SC
is an alias for class
System.Console
.
Keyword
Alias
Namespace
↓
↓
↓
using Syst = System;
using SC =
System.Console
;
↑
↑
↑
Keyword
Alias
Class
The following code uses these aliases. All three lines of code in
Main
call the
System.Console.WriteLine
method.
The first statement in
Main
uses the alias for a
namespace
—
System
.
The second statement uses the
fully qualified name
of the method.
The third statement uses the alias for a
class
—
Console
.
using Syst = System; // using alias directive
using SC = System.Console; // using alias directive
namespace MyNamespace
{
class SomeClass
{
static void Main()
{
Alias for namespace
↓
Syst.Console.WriteLine ("Using the namespace alias.");
System.Console.WriteLine("Using fully qualified name.");
SC.WriteLine ("Using the type alias");
↑
}
Alias for class
}
}