Quiet read CLR via C # (06)-Constructor
Recently, I am busy watching the new pearl and have not studied for several days. It is a sin and a sin.
Today we summarize the knowledge of the class constructor.ArticleThere are quite a few, but I still feel that writing my own thoughts and understandings will improve.
You should be familiar with constructors. They are mainly used for initialization. There are two types of constructor: instance constructor and class constructor. Let's take a look at the actual example.
Class dog: Animal
{
Field
Private String _ Name;
Private Int _ Age;
Public String Name // Attribute
{
Get { Return _ Name ;}
Set {_ Name = Value ;}
}
Public Int Age
{
Get { Return _ Age ;}
Set {_ Age = Value ;}
}
Public Static StringType= "Animals";//Static Field
Instance Constructor
Public Dog () // ① No parameter instance Constructor
{
_ Name = " Unknown " ;
}
Public Dog ( String Name) // ② Overload, constructor with Parameters
{
This . _ Name = Name;
}
Public Dog ( String Name, Int Age) // ③ Explicitly call other constructors
: This ()
{
_ Age = Age;
}
Class Constructor
Static Dog () // Class 4 Constructor
{
Type = " Dog " ;
}
}
Base class animal
Class Animal // Base Class
{
Public Animal ()
{
Console. writeline ( " I am an animal. " );
}
}
An instance Constructor
The instance constructor is mainly responsible for initializing instances of the type to a reasonable state. The instance constructor of the reference type and value type is different.
1.
Reference Type instance Constructor
The instance constructor can be overloaded and has different access restrictions. In the above example, ① ② ③ is a reference type instance constructor.
The instance constructor and class name are the same, but no response type is returned. In ildasm.exe, check as. ctor.
If we do not define an instance constructor, the compiler will generate a non-argument constructor by default.
Instance object initialization process
- Allocate memory for the instance;
- Initialize additional members, including the method table pointer and syncblockindex variable (we have alreadyReading CLR via C #(03)).
- Call the instance constructor for initialization.
Before calling the constructor, the variable is initialized to 0 or null, so the variable not changed by the constructor will remain 0 after the instance is created. For example, the following age field keeps the value 0
Call Sequence
If the class does not display the definition constructor, the compiler automatically generates a non-argument constructor that calls the base class's no-argument constructor. For example
Public class animal {}
Equivalent
Public class animal
{
Public animal (): Base (){}
}
If the class modifier is static (sealed and abstract), the compiler does not generate the constructor by default;
If the base class does not provide a non-argument constructor, the derived class must call a constructor. Otherwise, the compilation is incorrect.
If there is an inheritance relationship, the derived class should call the constructors of the base class before using the fields of the base class. If the derived class does not explicitly call the base class constructor, the compiler automatically generatesCode, Along the inheritance level until the no-argument constructor location of system. object. For example, the following is the result of calling the dog = new dog () method.
Class dog: animal...
Dog () method il code
Code explosion?
To prevent code expansion caused by a large number of repeated values during reload of the constructor, we recommend that you place public initialization statements in a constructor, and other constructor explicitly calls the constructor.
2.
Value Type instance Constructor
- The value type does not have a default no-argument constructor, and we cannot define a no-argument constructor. However, we can customize constructors with parameters.
- Initialization of the inline instance field in the value type is not allowed. The following example produces a compilation error.StructTeststruct
{
Partial IntNumber=5;
}
- The Value Type constructor must Initialize all instance fields. If a variable is not initialized, an error is returned.
If you do not want to initialize all fields one by one, there is an alternative solution:
Struct Dog
{
Public Int Age;
Public String Name;
Public Dog ( String Name)
{
This = New Dog ();
Name = Name;
}
}
In the Value Type constructor, this represents an instance of the value type itself. When a value type instance created with new is assigned to this, all fields are set to zero. Therefore, this solution can be compiled.
- After a constructor with parameters is defined, it must be explicitly called with new before execution. Otherwise, fields of the value type will remain 0 or null.
Class 2 Constructor
- The class constructor is applicable to reference types (including interfaces) and value types. It is used to set the initial State of a class. There is no default class constructor in the class. We need to explicitly construct it and mark it as a static method. Corresponds to. cctor in the metadata table
- The class constructor can have only one and cannot be overloaded. Parameters cannot be included. The purpose of the class constructor is to initialize static members of the class. It can only access static members and cannot access instance members.
- The access restrictions of the class constructor are private, but we cannot add access modifiers before the class constructor, or private, or else compilation errors may occur, this is done to prevent developers from calling this method. The CLR is responsible for its calls. We should avoid writing code that requires calling the class constructor in a specific order.
- Do not call the class constructor of the base class. Because the static member of the base class is not inherited by the derived class, it is only statically bound during compilation.
- The call sequence of the class constructor is similar to that of the Instance constructor. The static field is initialized first, and then assigned a value in the constructor. For example:
Class dog: Animal
{
Public static string type = "animal"; // static field
// Class Constructor
Static dog ()
{
Type = "dog ";
}
}
Console. writeline (dog. type );
This section is a short test.
What is the age field value of dog?
A.0 B .5 C. Others