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.
RemarksRemarks
通常は、静的メンバーを呼び出すときに、型名とメンバー名を指定します。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); }
}
}
メンバーが参照されるたびに Math クラスを明示的に参照する必要がなくなれば、using static
ディレクティブによって生成されるコードがより簡潔になります。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 static ディレクティブを使用して、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
ディレクティブを使用して、Console クラス、Math クラス、および String クラスの静的メンバーを、型名を指定せずに使用できるようにしています。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. ただし、どの数値型の TryParse
メソッドが呼び出されたかを判断するために using static
ディレクティブを確認する必要が出てくるため、コードが読みにくくなります。However, this creates less readable code, since it becomes necessary to check the using static
directives to determine which numeric type's TryParse
method is called.