共用方式為


static (C# Reference) 

Use the static modifier to declare a static member, which belongs to the type itself rather than to a specific object. The static modifier can be used with classes, fields, methods, properties, operators, events and constructors, but cannot be used with indexers, destructors, or types other than classes. For example, the following class is declared as static, and contains only static methods:

static class CompanyEmployee
{
    public static string GetCompanyName(string name) { ... }
    public static string GetCompanyAddress(string address) { ... }
}

For more information, see Static Classes and Static Class Members (C# Programming Guide).

Remarks

  • 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;
        }
    }
    

    To refer to the static member x, use the fully qualified name (unless it is accessible from the same scope):

    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.

  • It is not possible to use this to reference static methods or property accessors.

  • If the static keyword is applied to a class, all the members of the class must be static.

  • Classes, including static classes, may have static constructors. Static constructors are called at some point between when the program begins and the class is instantiated.

    Note

    The static keyword is more limited in use than in C++. To compare with the C++ keyword, see 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 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

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 as well as 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.

// cs_static_keyword.cs
using System;

public class Employee
{
    public string id;
    public string name;

    public Employee()
    {
    }

    public Employee(string name, string id)
    {
        this.name = name;
        this.id = id;
    }

    public static int employeeCounter;

    public static int AddEmployee()
    {
        return ++employeeCounter;
    }
}

class MainClass : Employee
{
    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:
        Employee e = new Employee(name, id);
        Console.Write("Enter the current number of employees: ");
        string n = Console.ReadLine();
        Employee.employeeCounter = Int32.Parse(n);
        Employee.AddEmployee();

        // Display the new information:
        Console.WriteLine("Name: {0}", e.name);
        Console.WriteLine("ID:   {0}", e.id);
        Console.WriteLine("New Number of Employees: {0}",
                      Employee.employeeCounter);
    }
}

Input

Tara Strahan
AF643G
15

Sample Output

Enter the employee's name: Tara Strahan
Enter the employee's ID: AF643G
Enter the current number of employees: 15
Name: Tara Strahan
ID:   AF643G
New Number of Employees: 16

This example shows that while it is possible to initialize a static field with another static field not yet declared, the results will be undefined, until you explicitly assign a value to the static field.

// cs_static_keyword_2.cs
using System;
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# Language Specification

For more information, see the following sections in the C# Language Specification:

  • 1.6.5.3 Static and instance methods

  • 5.1.1 Static variables

  • 10.2.5 Static and instance members

  • 10.4.1 Static and instance fields

  • 10.4.5.1 Static field initialization

  • 10.5.2 Static and instance methods

  • 10.6.1 Static and instance properties

  • 10.7.3 Static and instance events

  • 10.11 Static constructors

See Also

Reference

C# Keywords
Modifiers (C# Reference)
Static Classes and Static Class Members (C# Programming Guide)

Concepts

C# Programming Guide

Other Resources

C# Reference