Static constructor,
1, for the initialization of static data
2, the preceding cannot have the parameter cannot have the modifier, cannot be called
Class program { private static int count = 0; static program () { console.writeline ("I'm a static constructor, I'm loaded!") "); count++; console.writeline (count); } public program () { Console.WriteLine ("I'm a public constructor, I'm loaded!") "); count++; } &nbsP;static void main (String[] args) { program o1 = new program ();// Here two constructors call console.writeline ("------- ---"); program o2 = new program ();//Call only the second constructor Console.WriteLine (count); console.read ( ); } } /* code Run Results: I'm a static constructor, I'm loaded! 1 I'm a public constructor, I'm loaded! ---------- I'm a public constructor, I'm loaded! 3 */
There is no access modifier in front of the constructor, the default is public, if you change to a modifier such as private and protected is not possible, because private access can only make this class, This means that if an object outside of this class is to be instantiated, the other is not.
C # static Static constructor