共用方式為


static (C# 參考資料)

此頁面涵蓋 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 修飾詞新增至 Lambda 運算式匿名方法。 靜態 Lambda 或匿名方法無法擷取局部變數或實例狀態。

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 字段只有一個複本。

無法用來 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
*/

範例 - 靜態初始化

此範例示範您可以使用尚未宣告的另一個staticstatic欄位來初始化欄位。 在您明確將值指派給 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# 語法和使用方式的最終來源。

另請參閱