using static 指示詞 (C# 參考)using static directive (C# Reference)

using static 指示詞指定一種類型,讓您不需要指定類型名稱,即可存取其靜態成員和巢狀型別。The using static directive designates a type whose static members and nested types you can access without specifying a type name. 它的語法為:Its syntax is:

using static <fully-qualified-type-name>;

其中 fully-qualified-type-name 是不需要指定類型名稱,即可參考其靜態成員和巢狀型別類型的名稱。where fully-qualified-type-name is the name of the type whose static members and nested types can be referenced without specifying a type name. 如果您未提供完整類型名稱 (完整命名空間名稱及類型名稱),C# 會產生編譯器錯誤 CS0246:「找不到類型或命名空間名稱 'type/namespace' (您是否遺漏 using 指示詞或組件參考?)」。If you do not provide a fully qualified type name (the full namespace name along with the type name), C# generates compiler error CS0246: "The type or namespace name 'type/namespace' could not be found (are you missing a using directive or an assembly reference?)".

using static 指示詞會套用至任何具有靜態成員 (或巢狀型別) 的類型,即使該類型同時具有執行個體成員也一樣。The using static directive applies to any type that has static members (or nested types), even if it also has instance members. 不過,執行個體成員只能透過類型執行個體叫用。However, instance members can only be invoked through the type instance.

using static 指示詞是在 C# 6 中引進。The using static directive was introduced in C# 6.

備註Remarks

一般而言,當您呼叫靜態成員時,您會提供類型名稱及成員名稱。Ordinarily, when you call a static member, you provide the type name along with the member name. 重複輸入相同的類型名稱來叫用類型的成員,可能會導致冗長且難以理解的程式碼。Repeatedly entering the same type name to invoke members of the type can result in verbose, obscure code. 例如,Circle 類別的下列定義參考了 Math 類別的一些成員。For example, the following definition of a Circle class references a number of members of the Math class.

using System;

public class Circle
{
   public Circle(double radius)
   {
      Radius = radius;
   }

   public double Radius { get; set; }

   public double Diameter
   {
      get { return 2 * Radius; }
   }

   public double Circumference
   {
      get { return 2 * Radius * Math.PI; }      
   }

   public double Area
   {
      get { return Math.PI * Math.Pow(Radius, 2); }
   }
}

using static 指示詞可避免每次參考成員時都必須明確參考 Math 類別,因此所產生的程式碼會更簡潔:By eliminating the need to explicitly reference the Math class each time a member is referenced, the using static directive produces much cleaner code:

using System;
using static System.Math;

public class Circle
{
   public Circle(double radius)
   {
      Radius = radius;
   }

   public double Radius { get; set; }

   public double Diameter
   {
      get { return 2 * Radius; }
   }

   public double Circumference
   {
      get { return 2 * Radius * PI; }      
   }

   public double Area
   {
      get { return PI * Pow(Radius, 2); }
   }
}

using static 只會匯入指定的類型中宣告的可存取靜態成員和巢狀類型。using static imports only accessible static members and nested types declared in the specified type. 不會匯入繼承的成員。Inherited members are not imported. 您可以使用 using 靜態指示詞從任何具名類型匯入,包括 Visual Basic 模組。You can import from any named type with a using static directive, including Visual Basic modules. 如果 F# 最上層函式出現在中繼資料中,作為具名類型的靜態成員,其名稱是有效的 C# 識別項,則可以匯入 F# 函式。If F# top-level functions appear in metadata as static members of a named type whose name is a valid C# identifier, then the F# functions can be imported.

using static 讓您在可供擴充方法查閱使用的指定類型中宣告擴充方法。using static makes extension methods declared in the specified type available for extension method lookup. 不過,擴充方法的名稱不會匯入程式碼中未限定參考的範圍。However, the names of the extension methods are not imported into scope for unqualified reference in code.

在相同編譯單位或命名空間中透過不同 using static 指示詞從不同的類型匯入、具有相同名稱的方法會形成方法群組。Methods with the same name imported from different types by different using static directives in the same compilation unit or namespace form a method group. 這些方法群組內的多載解析會遵循一般的 C# 規則。Overload resolution within these method groups follows normal C# rules.

範例Example

下列範例使用 using static 指示詞來提供 ConsoleMathString 類別的靜態成員,而不需要指定其類型名稱。The following example uses the using static directive to make the static members of the Console, Math, and String classes available without having to specify their type name.

using System;
using static System.Console;
using static System.Math;
using static System.String;

class Program
{
   static void Main()
   {
      Write("Enter a circle's radius: ");
      var input = ReadLine();
      if (!IsNullOrEmpty(input) && double.TryParse(input, out var radius)) {
         var c = new Circle(radius);
         
         string s = "\nInformation about the circle:\n";
         s = s + Format("   Radius: {0:N2}\n", c.Radius);
         s = s + Format("   Diameter: {0:N2}\n", c.Diameter);
         s = s + Format("   Circumference: {0:N2}\n", c.Circumference);
         s = s + Format("   Area: {0:N2}\n", c.Area);
         WriteLine(s);
      }
      else {
         WriteLine("Invalid input...");
      }
   }
}

public class Circle
{
   public Circle(double radius)
   {
      Radius = radius;
   }

   public double Radius { get; set; }

   public double Diameter
   {
      get { return 2 * Radius; }
   }

   public double Circumference
   {
      get { return 2 * Radius * PI; }      
   }

   public double Area
   {
      get { return PI * Pow(Radius, 2); }
   }
}
// The example displays the following output:
//       Enter a circle's radius: 12.45
//       
//       Information about the circle:
//          Radius: 12.45
//          Diameter: 24.90
//          Circumference: 78.23
//          Area: 486.95

在此範例中,using static 指示詞也可能已套用至 Double 類型。In the example, the using static directive could also have been applied to the Double type. 因此不需要指定類型名稱,即可呼叫 TryParse(String, Double) 方法。This would have made it possible to call the TryParse(String, Double) method without specifying a type name. 不過,這會建立難以閱讀的程式碼,因為您必須檢查 using static 陳述式,以判斷呼叫了哪一個數字類型的 TryParse 方法。However, this creates less readable code, since it becomes necessary to check the using static statements to determine which numeric type's TryParse method is called.

另請參閱See also