static (C# 參考)

本頁面涵蓋 static 修飾元關鍵字。 static 關鍵字也是 using static 指示詞的一部分。

使用 static 修飾詞來宣告靜態成員,而靜態成員屬於類型本身,而不是特定物件。 static 修飾元可用來宣告 static 類別。 在類別、介面和結構中,您可以將 static 修飾元新增至欄位、方法、屬性、運算子、事件和建構函式。 static 修飾元不能與索引子或完成項搭配使用。 如需詳細資訊,請參閱靜態類別和靜態類別成員

您可以將 static 修飾元新增至本機函式。 靜態區域函式無法擷取區域變數或執行個體狀態。

您可以將 static 修飾元新增至 Lambda 運算式匿名方法。 靜態 Lambda 或匿名方法無法擷取區域變數或執行個體狀態。

範例 - 靜態類別

下列類別宣告為 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 欄位只有一份複本。

無法使用 this 來參考 static 方法或屬性存取子。

如果將 static 關鍵字套用至類別,則類別的所有成員都必須是 static

類別、介面和 static 類別可能有 static 建構函式。 在程式開始後、類別具現化前的某個時間點,會呼叫 static 建構函式。

注意

static 關鍵字的使用方式比在 C++ 中受到更多限制。 若要與 C++ 關鍵字進行比較,請參閱儲存類別 (C++)

若要展示 static 成員,請考慮使用代表公司員工的類別。 假設此類別包含可計算員工人數的方法以及可儲存員工人數的欄位。 方法和欄位都不屬於任何一個員工執行個體, 而是屬於整個員工類別。 因此,應該宣告為類別的 static 成員。

範例 - 靜態欄位和方法

這個範例會讀取新員工的名稱和識別碼,並將員工計數器遞加一,然後顯示新員工和新員工人數的資訊。 此程式會從鍵盤讀取目前的員工人數。

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# 語法及用法的限定來源。

另請參閱