static(C# 参考)static (C# Reference)

使用 static 修饰符可声明属于类型本身而不是属于特定对象的静态成员。Use the static modifier to declare a static member, which belongs to the type itself rather than to a specific object. static 修饰符可用于类、字段、方法、属性、运算符、事件和构造函数,但不能用于索引器、终结器或类以外的类型。The static modifier can be used with classes, fields, methods, properties, operators, events, and constructors, but it cannot be used with indexers, finalizers, or types other than classes. 有关详细信息,请参阅静态类和静态类成员For more information, see Static Classes and Static Class Members.

示例Example

下面的类声明为 static 并且只含 static 方法:The following class is declared as static and contains only static methods:

static class CompanyEmployee
{
    public static void DoSomething() { /*...*/ }
    public static void DoSomethingElse() { /*...*/  }
}

常量或类型声明是隐式的静态成员。A constant or type declaration is implicitly a static member.

不能通过实例引用静态成员。A static member cannot be referenced through an instance. 然而,可以通过类型名称引用它。Instead, it is referenced through the type name. 例如,请考虑以下类:For example, consider the following class:

public class MyBaseC
{
    public struct MyStruct
    {
        public static int x = 100;
    }
}

若要引用静态成员 x,除非可从相同范围访问该成员,否则请使用完全限定的名称 MyBaseC.MyStruct.xTo refer to the static member x, use the fully qualified name, MyBaseC.MyStruct.x, unless the member is accessible from the same scope:

Console.WriteLine(MyBaseC.MyStruct.x);

尽管类的实例包含该类的所有实例字段的单独副本,但每个静态字段只有一个副本。While an instance of a class contains a separate copy of all instance fields of the class, there is only one copy of each static field.

不可以使用 this 引用静态方法或属性访问器。It is not possible to use this to reference static methods or property accessors.

如果 static 关键字应用于类,则类的所有成员都必须是静态的。If the static keyword is applied to a class, all the members of the class must be static.

类和静态类可以包含静态构造函数。Classes and static classes may have static constructors. 在程序开始和实例化类之间的某个时刻调用静态构造函数。Static constructors are called at some point between when the program starts and the class is instantiated.

备注

static 关键字比用于 C++ 中时受到的限制更多。The static keyword has more limited uses than in C++. 若要与 C++ 关键字进行比较,请参阅 Storage classes (C++)(存储类 (C++))。To compare with the C++ keyword, see Storage classes (C++).

若要演示静态成员,请考虑表示公司员工的类。To demonstrate static members, consider a class that represents a company employee. 假定此类包含计数员工的方法和存储员工人数的字段。Assume that the class contains a method to count employees and a field to store the number of employees. 方法和字段均不属于任何实例员工。Both the method and the field do not belong to any instance employee. 而属于公司类。Instead they belong to the company class. 因此,它们应声明为类的静态成员。Therefore, they should be declared as static members of the class.

示例Example

此示例读取新员工的姓名和 ID,员工计数器按 1 递增,并显示新员工信息和新员工人数。This example reads the name and ID of a new employee, increments the employee counter by one, and displays the information for the new employee and the new number of employees. 为简单起见,此程序从键盘读取员工的当前人数。For simplicity, this program reads the current number of employees from the keyboard. 在实际应用程序中,应从文件读取该信息。In a real application, this information should be read from a file.

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
*/

示例Example

此示例显示,尽管可以使用尚未声明的其他静态字段来初始化某个静态字段,但除非向该静态字段显式分配值,否则不会定义该结果。This example shows that although you can initialize a static field by using another static field not yet declared, the results will be undefined until you explicitly assign a value to the static field.

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# language specification

有关详细信息,请参阅 C# 语言规范For more information, see the C# Language Specification. 该语言规范是 C# 语法和用法的权威资料。The language specification is the definitive source for C# syntax and usage.

请参阅See also