Effective C USES a member initializer instead of an assignment statement

  • 2020-05-17 05:21:55
  • OfStack

In general, a class has multiple constructors. Over time, the number of member variables and constructors increases. The most convenient way to handle this situation is to initialize the variables when they are declared, rather than in each constructor. Whether the class members (static variables) are appropriate instance variables, we should take full advantage of the initializer's syntax.

C# programming in, 1 usually declare a variable while we initialize it:

1 class Employee 
2 { 
3 private List<Employee> empList = new List<Employee>(); 
4 }

No matter how many constructors we add to the Employee class, the empList variable is initialized correctly because:

The compiler generates code at the beginning of all constructors (including the default constructor) to define initializers for all instance member variables. So we don't need to add initialization code to the constructor for every member variable we define -- just initialize it at declaration time.

The initializer can be thought of as another representation of the initializing statement in the constructor. The code generated by the initializer is inserted before the constructor code. The initializer is executed before the base class constructor is called for the type execution, in the same order as the class member variables are declared.

The initializer syntax for C# is one of the easiest ways to avoid having uninitialized variables in a type. However, initializers should be avoided in the following three situations:

1. When the initialization object is 0 or null
Because the system default initialization will set the 1 cut to 0 or null(value type and reference type) before all code is executed. Since this step is a very low-level implementation, we can also set the object assignment to 0 or null directly, but this is obviously unnecessary.

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:

class Employee 
{ 
// Declare variables while initializing them  
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 collection that initializes the specified size, we actually create two List < Employee > . The first one becomes garbage immediately after creation - this is because the initializer will execute before all constructors. The code generated by the compiler looks like this:

class Employee 
{ 
// Declare a variable  
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 efficiency of the program and create unnecessary objects, so if you need to perform different initializations in different constructors, the right thing to do is not apply the initializer, but declare the variables first, and then initialize the member variables in the constructor, as follows:

class Employee 
{ 
// Declare a variable  
private List<Employee> empList; 

public Employee() 
{ 
empList = new List<Employee>(); 
} 

public Employee(int size) 
{ 
empList = new List<Employee>(size); 
} 
}

3. Exception handling is required
Initializer cannot be wrapped by try statement. Therefore, any exception that occurs during the execution of the object initializer will be passed out of the object. If you're initializing an object that might throw an exception, you should put that part of the code in the constructor and handle that exception. This enables the necessary recovery code to create type instances and handle exceptions in a more friendly manner.

Section:
A member initializer is the easiest way to ensure that all member variables in a type are initialized -- it initializes a variable when it is declared, and it executes before all constructors, regardless of which constructor is called. This syntax also avoids missing important initialization code when adding new constructors. So, if the initialization value of a member variable is one for all constructors, use the initializer syntax whenever possible.

Related articles: