Public class test
{
Public static int cc = 0;
Static Test ()
{
CC ++;
}
Public test ()
{
CC ++;
}
}
Test T1 = new test1 ();
Test t2 = new Test2 ();
Output: CC: 3
Analysis:
1: The static constructor neither has an access modifier nor a parameter. Because it is called by. net, modifiers such as public and private are meaningless.
2: when the first class instance or any static member is referenced ,. net will automatically call the static constructor to initialize the class. That is to say, we cannot directly call the static constructor, so we cannot control when to execute the static constructor.
3: A class can have only one static constructor.
4: constructors without parameters can coexist with static constructors. Although the parameter list is the same, one belongs to the class and the other belongs to the instance, so there is no conflict.
5: Run at most once.
6: static constructors cannot be inherited.
7. If no static constructor is written and the class contains static members with an initial value, the compiler automatically generates the default static constructor.