Static constructors are a new feature of C # that seems to be rarely used. However, when we want to initialize some static variables, we need to use it. This constructor belongs to the class, not to where the instance belongs, which means that the constructor is only executed once. This is called automatically by. NET before the first instance is created or any static members are referenced.
Class Simpleclass
{
Static Constructor
Static Simpleclass ()
{
//...
}
}
There are a few things you should be aware of when using static constructors:
1. The static constructor has neither an access modifier nor a parameter. Because it is. NET call, so modifiers like public and private are meaningless.
2, when creating the first class instance or any static member is referenced. NET automatically invokes the static constructor to initialize the class, which means that we cannot call the static constructor directly, and we cannot control when the static constructor is executed.
3. A class can have only one static constructor.
4. Parameterless constructors can coexist with static constructors. Although the argument list is the same, but one belongs to the class and one belongs to the instance, there is no conflict.
5, run up to only once.
6. Static constructors cannot be inherited.
7. If no static constructor is written, and the class contains static members with initializer settings, the compiler automatically generates the default static constructor.