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

本页介绍 static 修饰符关键字。This page covers the static modifier keyword. static 关键字也是 using static 指令的一部分。The static keyword is also part of the using static directive.

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

示例 - 静态类Example - static class

下面的类声明为 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() { /*...*/  }
}

常数或类型声明是隐式的 static 成员。A constant or type declaration is implicitly a static member. 不能通过实例引用 static 成员。A static member can't be referenced through an instance. 然而,可以通过类型名称引用它。Instead, it's referenced through the type name. 例如,请考虑以下类:For example, consider the following class:

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

若要引用 static 成员 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);

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

不可以使用 this 引用 static 方法或属性访问器。It isn't possible to use this to reference static methods or property accessors.

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

类、接口和 static 类可以具有 static 构造函数。Classes, interfaces, and static classes may have static constructors. 在程序开始和实例化类之间的某个时刻调用 static 构造函数。A static constructor is 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++).

若要演示 static 成员,请考虑表示公司员工的类。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 don't belong to any one employee instance. 相反,它们属于全体员工这个类。Instead, they belong to the class of employees as a whole. 应将其声明为该类的 static 成员。They should be declared as static members of the class.

示例 - 静态字段和方法Example - static field and method

此示例读取新员工的姓名和 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. 此程序从键盘读取员工的当前人数。This program reads the current number of employees from the keyboard.

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 - static initialization

此示例演示了如何使用尚未声明的 static 字段来初始化另一个 static 字段。This example shows that you can initialize a static field by using another static field that is not yet declared. 在向 static 字段显式赋值之后才会定义结果。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