Static class& class init
Today, when i read some part of the system, i found a static class, so i record here
static class or non static class can has static constructor.
static className{...}
When before create instance or use function member, Static Construct run and only run once.
For some add info: as follow
Static members are initialized before the static member is accessed for the first time and before the static constructor, if
there is one, is called.
TestStatic ts = new TestStatic();
public class TestStatic
{
private static int teststaticint = 1; // first init
private int testint = 2; //third init
static TestStatic() // second init
{
teststaticint = 10;
}
public TestStatic() //fourth init
{
testint = 20;
teststaticint = 30;
}
public static void PrintfMe()
{
Console.WriteLine("in static method");
}
}
浙公网安备 33010602011771号