c reference type constructor

  • 2020-05-07 20:19:11
  • OfStack

When creating an instance of a reference type,

First, allocate memory for the instance's data fields,

Then initialize the additional fields of the object (object pointer, synchronized block index),

Finally, the instance constructor defined in the type is called to set the initialization state of the object.

When constructing an object of a reference type, the memory allocated for all fields of the object is always zeroed or null before the instance constructor of the type is called.

The instance constructor can never be continued. If there is no explicit constructor in the defined class,
The c# compiler defines a default constructor that, in its implementation, simply calls the parameter free constructor of the base class.
So:
public class SomeType{}
Is equivalent to
public class SomeTyoe{
public SomeType():base(){}
}
If the class modifier is abstract, then the default constructor generated by the compiler is protected, otherwise public.
If the base class does not provide a parameter-free constructor, the derived class must display a call to one of the base class constructors, or the compiler will report an error.
If it is a static class, the class has no instance constructor.
The constructor of the base class must be called before the fields of the base class can be accessed in the class. If the constructor of the base class is not explicitly called,
The c# compiler automatically generates a call to the default base class constructor, which ends up calling Object's constructor.

Related articles: