Generally, a class has multiple constructors. Over time, the number of member variables and constructors has increased. The most convenient way to deal with this situation is to initialize the variable while declaring it, rather than in every constructor. Whether it is a class member (static variable), we should make full use of the initialization tool syntax.
C # programming in, we usually initialize a variable while declaring it:
Copy codeThe Code is as follows:
1 class Employee
2 {
3 private List <Employee> empList = new List <Employee> ();
4}
The empList variable can be correctly initialized no matter how many constructors we have added to the Employee class, because:
The compiler will generate code at the beginning of all Constructors (including Default constructors) to define the initializer (for initialization) for the member variables of the instance ); therefore, you do not need to add initialization code for each defined member variable in the constructor-initialize the variable directly during declaration.
The initializer can be seen as another representation of the initialization statement in the constructor. The code generated by the initialization tool is inserted before the constructor code for execution. The initialization tool will be executed before the base class constructor is called for Type execution. The sequence is the same as that of the class member variable declaration.
The C # initializer syntax is the simplest solution to avoid uninitialized variables in the type. However, in the following three cases, you should avoid using the initialization tool:
1. When the initialization object is 0 or null
Because the default system initialization will set everything to 0 or null (Value Type and reference type) before all code is executed ). In addition, this step is implemented at the very bottom layer. We can also directly set the object value to 0 or null, but this is obviously redundant.
2. Perform different initialization methods for the same variable
One premise for using initialization statements is that all constructors will set the same value for the variable. Let's look at the following sample code:
Copy codeThe Code is as follows:
Class Employee
{
// Declare the variable for initialization at the same time
Private List <Employee> empList = new List <Employee> ();
Public Employee ()
{
}
Public Employee (int size)
{
EmpList = new List <Employee> (size );
}
}
In the code above, when we call the second constructor to create a generic set of the specified size, two lists <Employee> are actually created. The first one immediately becomes garbage after being created-this is because the initiator will execute before all constructors. The code generated by the compiler is similar to the following code:
Copy codeThe Code is as follows:
Class Employee
{
// Declare Variables
Private List <Employee> empList;
Public Employee ()
{
EmpList = new List <Employee> ();
}
Public Employee (int size)
{
EmpList = new List <Employee> ();
EmpList = new List <Employee> (size );
}
}
We can see that this will affect the program efficiency and create unnecessary objects, therefore, if you need to execute different initialization methods in different constructors, the correct method should be not applicable to the initializer. Instead, you must declare the variables and then initialize the member variables in the constructor, as follows:
Copy codeThe Code is as follows:
Class Employee
{
// Declare Variables
Private List <Employee> empList;
Public Employee ()
{
EmpList = new List <Employee> ();
}
Public Employee (int size)
{
EmpList = new List <Employee> (size );
}
}
3. Exception Handling required
The initiator cannot be wrapped in a try statement. Therefore, exceptions during object initialization will be passed out of the object. If an exception may be thrown during object initialization, we should put the code in the constructor to handle the exception. In this way, necessary recovery code can be implemented to create type instances and handle exceptions in a more friendly way.
Section:
The member initializer is the simplest way to ensure that all member variables of the type are initialized-initialize the member variables when they are declared, no matter which constructor is called, the initialization tool will be executed before all constructors. This syntax also avoids Missing Important initialization code when adding a new constructor. Therefore, if the initialization values of a member variable of all constructors are the same, you should try to use the initialization tool syntax.