using 指令(C# 参考)using directive (C# Reference)

using 指令有三种用途: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;
    

    有关详细信息,请参阅 using static 指令For more information, see the using static directive.

  • 为命名空间或类型创建别名。To create an alias for a namespace or a type. 这称为 using 别名指令This is called a using alias directive.

    using Project = PC.MyCompany.Project;
    

using 关键字还用于创建 using 语句,此类语句有助于确保正确处理 IDisposable 对象(如文件和字体) 。The using keyword is also used to create using statements, which help ensure that IDisposable objects such as files and fonts are handled correctly. 有关详细信息,请参阅 using 语句See using Statement for more information.

Using 静态类型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

using 指令的范围限于显示它的文件。The scope of a using directive is limited to the file in which it appears.

可能出现 using 指令的位置: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.

否则,将生成编译器错误 CS1529Otherwise, compiler error CS1529 is generated.

创建 using 别名指令,以便更易于将标识符限定为命名空间或类型。Create a using alias directive to make it easier to qualify an identifier to a namespace or type. 在任何 using 指令中,都必须使用完全限定的命名空间或类型,而无需考虑它之前的 using 指令。In any using directive, the fully-qualified namespace or type must be used regardless of the using directives that come before it. using 指令的声明中不能使用 using 别名。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;

创建 using 指令,以便在命名空间中使用类型而不必指定命名空间。Create a using directive to use the types in a namespace without having to specify the namespace. using 指令不为你提供对嵌套在指定命名空间中的任何命名空间的访问权限。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. 有关系统定义的命名空间的列表,请参阅 .NET API 浏览器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.

示例 1Example 1

下面的示例显示如何为命名空间定义和使用 using 别名: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
            var mc = new Project.MyClass();
        }
    }
    namespace MyCompany
    {
        namespace Project
        {
            public class MyClass { }
        }
    }
}

using 别名指令的右侧不能有开放式泛型类型。A using alias directive cannot have an open generic type on the right hand side. 例如,不能为 List<T> 创建 using 别名,但可以为 List<int> 创建 using 别名。For example, you cannot create a using alias for a List<T>, but you can create one for a List<int>.

示例 2Example 2

下面的示例显示如何为类定义 using 指令和 using 别名: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()
        {
            var instance1 = new AliasToMyClass();
            Console.WriteLine(instance1);

            var instance2 = new UsingAlias();
            Console.WriteLine(instance2);

        }
    }
}
// Output: 
//    You are in NameSpace1.MyClass.
//    You are in NameSpace2.MyClass.

C# 语言规范C# language specification

有关详细信息,请参阅 C# 语言规范中的 Using 指令For more information, see Using directives in the C# Language Specification. 该语言规范是 C# 语法和用法的权威资料。The language specification is the definitive source for C# syntax and usage.

请参阅See also