using directive (C# Reference)
The using
directive has three uses:
To allow the use of types in a namespace so that you do not have to qualify the use of a type in that namespace:
using System.Text;
To allow you to access static members and nested types of a type without having to qualify the access with the type name.
using static System.Math;
For more information, see the using static directive.
To create an alias for a namespace or a type. This is called a using alias directive.
using Project = PC.MyCompany.Project;
The using
keyword is also used to create using statements, which help ensure that IDisposable objects such as files and fonts are handled correctly. See using Statement for more information.
Using static type
You can access static members of a type without having to qualify the access with the type name:
using static System.Console;
using static System.Math;
class Program
{
static void Main()
{
WriteLine(Sqrt(3*3 + 4*4));
}
}
Remarks
The scope of a using
directive is limited to the file in which it appears.
The using
directive can appear:
- At the beginning of a source code file, before any namespace or type definitions.
- In any namespace, but before any namespace or types declared in this namespace.
Otherwise, compiler error CS1529 is generated.
Create a using
alias directive to make it easier to qualify an identifier to a namespace or type. In any using
directive, the fully-qualified namespace or type must be used regardless of the using
directives that come before it. No using
alias can be used in the declaration of a using
directive. For example, the following generates a compiler error:
using s = System.Text;
using s.RegularExpressions;
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 are nested in the namespace you specify.
Namespaces come in two categories: user-defined and system-defined. User-defined namespaces are namespaces defined in your code. For a list of the system-defined namespaces, see .NET API Browser.
For examples on referencing methods in other assemblies, see Create and Use Assemblies Using the Command Line.
Example 1
The following example shows how to define and use a using
alias for a namespace:
namespace PC
{
// Define an alias for the nested namespace.
using Project = PC.MyCompany.Project;
class A
{
void M()
{
// Use the alias
Project.MyClass mc = new Project.MyClass();
}
}
namespace MyCompany
{
namespace Project
{
public class MyClass { }
}
}
}
A using alias directive cannot have an open generic type on the right hand side. For example, you cannot create a using alias for a List<T>
, but you can create one for a List<int>
.
Example 2
The following example shows how to define a using
directive and a using
alias for a class:
using System;
// Using alias directive for a class.
using AliasToMyClass = NameSpace1.MyClass;
// Using alias directive for a generic class.
using UsingAlias = NameSpace2.MyClass<int>;
namespace NameSpace1
{
public class MyClass
{
public override string ToString()
{
return "You are in NameSpace1.MyClass.";
}
}
}
namespace NameSpace2
{
class MyClass<T>
{
public override string ToString()
{
return "You are in NameSpace2.MyClass.";
}
}
}
namespace NameSpace3
{
class MainClass
{
static void Main()
{
AliasToMyClass instance1 = new AliasToMyClass();
Console.WriteLine(instance1);
UsingAlias instance2 = new UsingAlias();
Console.WriteLine(instance2);
}
}
}
// Output:
// You are in NameSpace1.MyClass.
// You are in NameSpace2.MyClass.
C# language specification
For more information, see Using directives in the C# Language Specification. The language specification is the definitive source for C# syntax and usage.
See also
Feedback
Send feedback about:
Loading feedback...