Change. Static constructors can't have parameters (meaning they can't be overloaded), can't have modifiers (static constructors are always private, so they can't have modifiers) and can't be called. When the class is loaded, its static constructor is
Automatic call. Declare with the static keyword
Running standard of static constructor:
1. During the execution of the program, the value of the static constructor is executed once.
2. The static constructor is executed after the static members of the class are initialized. Or the compiler will convert the static member initialization statement into an assignment statement and put it at the beginning of the static constructor execution. In the next class,
Level a
{
public static int I = 100;
}
To initialize this static member, this class will automatically create a static constructor. If I is not assigned a value, a static constructor is not automatically created.
3. Static constructors are executed before applying static members of any class.
Level a
{
public static int I = 100;
Static a ()
{
Console. WriteLine(" static ");
}
Public a ()
{
Console. WriteLine ("dynamic");
}
}
Class plan
{
Static void Main(string[] args)
{
Console. WriteLine (artificial intelligence);
Console. ReadLine();
}
The printed result of {is
static electricity
100 () illustrates the third point.
4. Static constructors are executed before assigning instance variables of any class.
Level a
{
public static int I = 100;
Static a ()
{
Console. WriteLine(" static ");
}
Public a ()
{
Console. WriteLine ("dynamic");
}
}
Class plan
{
Static void Main(string[] args)
{
A A = new A();
A a 1 = new a ();
Console. ReadLine();
}
} the print result is
static electricity
dynamic
Dynamic (explained the first and fourth points)
The main function of a static constructor is to initialize the static members of a class.