- Using system;
- Class circle
- {
- Private int R;
- Public circle ()
- {
- Console. writeline ("nonstatic ");
- }
- Static circle ()
- {
- Console. writeline ("static ");
- }
- Public static void main ()
- {
- Console. writeline ("1st line ");
- Circle X, Y; // 1st rows
- Console. writeline ("2nd line ");
- X = new circle (); // 2nd rows
- Console. writeline ("3rd line ");
- Y = new circle (); // 3rd rows
- }
- }
Description of static constructor:
(1) It is used to initialize static and read-only fields.
(2) You cannot add access modifiers when adding static keywords because static constructors are private.
(3) Static constructor of a class can be executed at most once in a given application domain: only the static constructor of the class is activated when an instance of the class is created or any static member of the referenced class is activated.
(4) Static constructors cannot be inherited and cannot be called directly.
(5) If the class contains the main method used to start execution, the static constructor of the class will be executed before calling the main method. For any static field with an initial value, the initial values must be executed in the text order before executing the static constructor of this class.
(6) If a static constructor is not compiled, and the class contains a static field with an initial value, the compiler automatically generates the default static constructor.
The program that starts with (5) knows and outputs the result:
Static
1st line
2nd Line
Nonstatic
3rd line
Nonstatic