A type constructor , also called a class constructor or static constructor , is used to initialize a type 类型构造函数(也称为类构造函数或静态构造函数)用于初始化类型。
Static constructors are also useful when creating wrapper classes for unmanaged code , when the constructor can call the 静态构造函数在为非托管代码创建包装类时也很有用,此时该构造函数可以调用
A static constructor does not take access modifiers or have parameters . a static constructor is called automatically to initialize the 在创建第一个实例或引用任何静态成员之前,将自动调用静态构造函数来初始化
Static members are initialized before the static member is accessed for the first time , and before the static constructor , if any is called 静态成员在第一次被访问之前并且在任何静态构造函数(如调用的话)之前初始化。
A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file 静态构造函数的典型用途是:当类使用日志文件时,将使用这种构造函数向日志文件中写入项。
Static constructors are called automatically , immediately before any static fields are accessed , and are normally used to initialize static class members 在访问任何静态字段之前,都将自动调用静态构造函数,它们通常用于初始化静态类成员。
Static classes cannot contain a constructor , although it is still possible to declare a static constructor to assign initial values or set up some static state 静态类不能包含构造函数,但仍可声明静态构造函数以分配初始值或设置某个静态状态。
As a result , when a link demand is applied to an entire class , it cannot protect access to a static constructor , although it does protect the rest of the class 因此,当链接要求应用于整个类时,它无法保护对静态构造函数的访问(尽管它会保护类中的其他部分) 。
Constructors are named . ctor , while static constructors aka static initializers , class constructors , etc are named . cctor . these are never renamed . for additional reference , see the 将构造函数命名为“ . ctor ” ,而将静态构造函数( aka静态初始值设定项、类构造函数等)命名为“ . cctor ” 。
For an instance field , in the instance constructors of the class that contains the field declaration , or for a static field , in the static constructor of the class that contains the field declaration 对于实例字段,在包含字段声明的类的实例构造函数中;或者,对于静态字段,在包含字段声明的类的静态构造函数中。