C# - 静态类、方法、构造函数、字段(Static Class, Methods, Constructors, Fields)

本文关键字:Class Methods Constructors Static Fields 静态类 方法 构造函数 字段 | 更新日期: 2023-09-12 17:38:42

在 C# 中,static 表示无法实例化的东西。不能创建静态类的对象,也不能使用对象访问静态成员。

可以使用 static 修饰符关键字将 C# 类、变量、方法、属性、运算符、事件和构造函数定义为静态。

static class

在类名之前和访问修饰符之后应用 static 修饰符以使类成为静态类。下面定义一个具有静态字段和方法的静态类。

Example: C# Static Class

public static class Calculator
{
    private static int _resultStorage = 0;
    
    public static string Type = "Arithmetic";
    public static int Sum(int num1, int num2)
    {
        return num1 + num2;
    }
    public static void Store(int result)
    {
        _resultStorage = result;
    }
}

上面Calculator类是静态的。它的所有成员也是静态的。

您不能创建静态类的对象;因此可以使用类名(如 ClassName.MemberName)直接访问静态类的成员,如下所示。

Example: Accessing Static Members

class Program
{
    static void Main(string[] args)
    {
        var result = Calculator.Sum(10, 25); // calling static method
        Calculator.Store(result); 
        var calcType = Calculator.Type; // accessing static variable
        Calculator.Type = "Scientific"; // assign value to static variable
    }
}

静态类的规则

  1. 无法实例化静态类。
  2. 静态类的所有成员都必须是静态的;否则编译器将给出错误。
  3. 静态类可以包含静态变量、静态方法、静态属性、静态运算符、静态事件和静态构造函数。
  4. 静态类不能包含实例成员和构造函数。
  5. 索引器和析构函数不能是静态的
  6. var也不能用于定义静态成员。必须在 static 关键字后显式指定成员类型。
  7. 静态类是密封类,因此不能继承。
  8. 静态类不能从其他类继承。
  9. 可以使用 ClassName.MemberName 访问静态类成员。
  10. 静态类在程序所在的应用程序域的生存期内保留在内存中。

非静态类中的静态成员

普通类(非静态类)可以包含一个或多个静态方法、字段、属性、事件和其他非静态成员。

定义具有一些静态成员的非静态类比将整个类声明为静态类更实用。

Static fields

可以使用

static 关键字定义非静态类中的静态字段。

非静态类的静态字段在所有实例之间共享。因此,一个实例所做的更改将反映在其他实例中。

Example: Shared Static Fields

public class StopWatch
{
    public static int NoOfInstances = 0;
    
    // instance constructor
    public StopWatch()
    {
        StopWatch.NoOfInstances++;
    }
}
class Program
{
    static void Main(string[] args)
    {
        StopWatch sw1 = new StopWatch();
        StopWatch sw2 = new StopWatch();
        Console.WriteLine(StopWatch.NoOfInstances); //2 
			
        StopWatch sw3 = new StopWatch();
        StopWatch sw4 = new StopWatch();
        Console.WriteLine(StopWatch.NoOfInstances);//4
    }
}

静态方法

可以在非静态类中定义一个或多个静态方法。 可以在不创建对象的情况下调用静态方法。不能使用非静态类的对象调用静态方法。

静态方法

只能调用其他静态方法并访问静态成员。不能在静态方法中访问类的非静态成员。

Example: Static Method

class Program
{
    static int counter = 0;
    string name = "Demo Program";
    static void Main(string[] args)
    {
        counter++; // can access static fields
        Display("Hello World!"); // can call static methods
        name = "New Demo Program"; //Error: cannot access non-static members
        SetRootFolder("C:MyProgram"); //Error: cannot call non-static method
    }
    static void Display(string text)
    {
        Console.WriteLine(text);
    }
    public void SetRootFolder(string path) {  }
}

静态方法的规则

  1. 可以在返回类型之前和访问修饰符之后使用 static 关键字定义静态方法。
  2. 静态方法可以重载,但不能重写。
  3. 静态方法可以包含局部静态变量。
  4. 静态方法无法访问或调用非静态变量,除非它们作为参数显式传递。

静态构造函数

非静态类可以包含无参数静态构造函数。可以使用 static 关键字定义它,而无需访问修饰符,如公共、私有和受保护。

下面的示例演示静态构造函数和实例构造函数之间的区别。

Example: Static Constructor vs Instance Constructor

public class StopWatch
{
    // static constructor
    static StopWatch()
    {
        Console.WriteLine("Static constructor called");
    }
    // instance constructor
    public StopWatch()
    {
        Console.WriteLine("Instance constructor called");
    }
    // static method
    public static void DisplayInfo()
    {
        Console.WriteLine("DisplayInfo called");
    }
    // instance method
    public void Start() { }
    // instance method
    public void Stop() {  }
}

如上所述,非静态类StopWatch包含一个静态构造函数和一个非静态构造函数。

每当使用静态方法或首次创建实例时,仅调用一次静态构造函数。下面的示例演示首次调用静态方法时调用静态构造函数。第二次调用静态方法不会调用静态构造函数。

Example: Static Constructor Execution

StopWatch.DisplayInfo(); // static constructor called here
StopWatch.DisplayInfo(); // none of the constructors called here

输出:

Static constructor called.
DisplayInfo called
DisplayInfo called

下面的示例演示首次创建实例时调用静态构造函数。

Example: Static Constructor Execution

StopWatch sw1 = new StopWatch(); // First static constructor and then instance constructor called 
StopWatch sw2 = new StopWatch();// only instance constructor called 
StopWatch.DisplayInfo();

输出:

Static constructor called
instance constructor called
instance constructor called
DisplayInfo called

静态构造函数的规则

  1. 静态构造函数是使用 static 关键字定义的,不使用公共、私有或受保护的访问修饰符。
  2. 非静态类可以包含一个无参数静态构造函数。不允许使用参数化的静态构造函数。
  3. 静态构造函数在生命周期中仅执行一次。因此,如果一个类在多个位置使用,则无法确定何时在应用程序中调用它。
  4. 静态构造函数只能访问静态成员。它不能包含或访问实例成员。

注意:静态成员存储在内存中称为高频堆的特殊区域中。非静态类的静态成员在类的所有实例之间共享。因此,一个实例所做的更改将反映在所有其他实例中。