Constructor is a special function used to initialize objects. The constructor starts execution during class instantiation and is used to initialize some default settings of the object.
Generally, if no constructor is provided, the compiler automatically creates a non-parameter constructor in the background. This is a very basic constructor, it can only Initialize all fields in the class to the default value of the corresponding type: for example, int type initialization 0 bool type initialization false; reference type Initialization is null.
Public ClassPerson {Private StringName;Private IntAge ;}
Person class
Once the constructor is provided, the compiler no longer automatically creates a default constructor:
Public ClassPerson {Private StringName;Private IntAge;PublicPerson (StringName,IntAge ){This. Name =Name;This. Age =Age ;}}
Constructors with Parameters
Another constructor is a static constructor which has the following features:
1. Only oneNo access modifier or ParameterStatic Constructor
2. when the class is loaded, the static constructor is executed only once, while the normal constructor starts to execute when the object is instantiated, that is to say, the static method is the first method to be executed in all constructors.
3. Static methods cannot be inherited
C # constructor of Classes