このページでは、 static
修飾子キーワードについて説明します。 static
キーワードは、using static
ディレクティブの一部でもあります。
static
修飾子を使用して、特定のオブジェクトではなく型自体に属する静的メンバーを宣言します。 static
修飾子を使用して、static
クラスを宣言できます。 クラス、インターフェイス、構造体では、フィールド、メソッド、プロパティ、演算子、イベント、コンストラクターに static
修飾子を追加できます。 static
修飾子は、インデクサーやファイナライザーでは使用できません。 詳細については、「 静的クラスと静的クラス メンバー」を参照してください。
static
修飾子をローカル関数に追加できます。 静的ローカル関数は、ローカル変数またはインスタンスの状態をキャプチャできません。
class Calc1
{
public void CalculateSum()
{
int a = 3;
int b = 7;
// Static local function - cannot access 'a' or 'b' directly
static int Add(int x, int y)
{
return x + y;
}
int result = Add(a, b);
Console.WriteLine($"Sum: {result}");
}
}
/*
Output:
Sum: 10
*/
ラムダ式または匿名メソッドにstatic
修飾子を追加できます。 静的ラムダまたは匿名メソッドでは、ローカル変数またはインスタンスの状態をキャプチャできません。
class Calc2
{
static void Main()
{
Func<int, int, int> add = static (a, b) => a + b;
int result = add(5, 10);
Console.WriteLine($"Sum: {result}");
}
}
/*
Output:
Sum: 15
*/
例 - 静的クラス
次のクラスは static
として宣言され、 static
メソッドのみが含まれています。
static class CompanyEmployee
{
public static void DoSomething() { /*...*/ }
public static void DoSomethingElse() { /*...*/ }
}
定数または型宣言は、暗黙的に static
メンバーです。 static
メンバーは、インスタンスを介して参照できません。 代わりに、型の名前を使って参照します。 たとえば、次のクラスについて考えてみます。
public class MyBaseC
{
public struct MyStruct
{
public static int x = 100;
}
}
static
メンバー x
を参照するには、同じスコープからメンバーにアクセスできる場合を除き、完全修飾名 (MyBaseC.MyStruct.x
) を使用します。
Console.WriteLine(MyBaseC.MyStruct.x);
クラスのインスタンスには、クラスのすべてのインスタンス フィールドの個別のコピーが含まれていますが、各 static
フィールドのコピーは 1 つだけです。
this
を使用してstatic
メソッドまたはプロパティ アクセサーを参照することはできません。
static
キーワードがクラスに適用される場合、クラスのすべてのメンバーがstatic
されている必要があります。
クラス、インターフェイス、および static
クラスには、 static
コンストラクターが含まれる場合があります。 static
コンストラクターは、プログラムの開始時からクラスのインスタンス化までの間のある時点で呼び出されます。
注
static
キーワードは、C++ よりも使用が制限されています。 C++ キーワードと比較するには、 ストレージ クラス (C++) を参照してください。
static
メンバーを説明するために、会社の従業員を表すクラスを検討します。 クラスに、従業員をカウントするメソッドと、従業員数を格納するフィールドが含まれているとします。 メソッドとフィールドの両方が 1 つの従業員インスタンスに属していません。 代わりに、従業員のクラス全体に属します。 クラスのメンバー static
として宣言する必要があります。
例 - 静的フィールドとメソッド
次の使用例は、新しい従業員の名前と ID を読み取り、従業員カウンターを 1 ずつ増やし、新しい従業員の情報と新しい従業員数を表示します。 このプログラムは、キーボードから現在の従業員数を読み取ります。
public class Employee4
{
public string id;
public string name;
public Employee4()
{
}
public Employee4(string name, string id)
{
this.name = name;
this.id = id;
}
public static int employeeCounter;
public static int AddEmployee()
{
return ++employeeCounter;
}
}
class MainClass : Employee4
{
static void Main()
{
Console.Write("Enter the employee's name: ");
string name = Console.ReadLine();
Console.Write("Enter the employee's ID: ");
string id = Console.ReadLine();
// Create and configure the employee object.
Employee4 e = new Employee4(name, id);
Console.Write("Enter the current number of employees: ");
string n = Console.ReadLine();
Employee4.employeeCounter = Int32.Parse(n);
Employee4.AddEmployee();
// Display the new information.
Console.WriteLine($"Name: {e.name}");
Console.WriteLine($"ID: {e.id}");
Console.WriteLine($"New Number of Employees: {Employee4.employeeCounter}");
}
}
/*
Input:
Matthias Berndt
AF643G
15
*
Sample Output:
Enter the employee's name: Matthias Berndt
Enter the employee's ID: AF643G
Enter the current number of employees: 15
Name: Matthias Berndt
ID: AF643G
New Number of Employees: 16
*/
例 - 静的初期化
この例では、まだ宣言されていない別のstatic
フィールドを使用して、static
フィールドを初期化できることを示します。 static
フィールドに値を明示的に割り当てるまで、結果は未定義になります。
class Test
{
static int x = y;
static int y = 5;
static void Main()
{
Console.WriteLine(Test.x);
Console.WriteLine(Test.y);
Test.x = 99;
Console.WriteLine(Test.x);
}
}
/*
Output:
0
5
99
*/
C# 言語仕様
詳細については、C# 言語仕様のを参照してください。 言語仕様は、C# の構文と使用法の決定的なソースです。
こちらも参照ください
.NET