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 alias 指示詞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. 如需詳細資訊,請參閱 sing 陳述式See using Statement for more information.

Using static 類型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.

範例 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 alias 指示詞的右邊不能有開放式的泛型類型。A using alias directive cannot have an open generic type on the right hand side. 例如,您無法為 List<T> 建立 using 別名,但是可以為 List<int> 建立。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