1. Private constructors
Because the constructor for the class is private, the class is instantiated in external code with the new keyword.
1 Public classStudent2 3 {4 5 Private stringname;6 7 PrivateStudent (stringName//the construction method of this class8 9 {Ten One This. name=name; A - } - the}
2. Static constructors
- The static constructor executes only once, and the static constructor is not sure exactly when it will be executed, but it is usually executed before the member of the class is first called.
- The static constructor does not add any access modifiers, because C # code never calls him, there is always. NET runtime calls him, so the addition of the access modifier makes no sense.
- Static constructors do not take any parameters.
- A class can have only one static constructor.
- A parameterless instance constructor and a static constructor coexist safely because the execution time is different, so there is no conflict between executions.
1 Public classStudent2 3 {4 5 StaticStudent ()//Static Constructors6 7 {8 9 //CodeTen One } A -}
Constructors in C #