ArticleDirectory
- 1. When the initialization object is 0 or null
- 2. Perform different initialization methods for the same variable
- 3. Exception Handling required
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:
1ClassEmployee2 {3PrivateList <employee> emplist =NewList <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 at the beginning of all Constructors (including Default constructors ).CodeSo we do not need to add initialization code for each defined member variable in the constructor-initialize it directly when declaring it.
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:
1 Class Employee 2 { 3 // Declare variables and initialize them at the same time. 4 Private List <employee> emplist = New List <employee> (); 5 6 Public Employee () 7 { 8 } 9 10 Public Employee ( Int Size) 11 { 12 Emplist = New List <employee> (Size ); 13 } 14 }
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:
1 Class Employee 2 { 3 // Declare Variables 4 Private List <employee> Emplist; 5 6 Public Employee () 7 { 8 Emplist = New List <employee> (); 9 } 10 11 Public Employee ( Int Size) 12 { 13 Emplist = New List <employee> (); 14 Emplist = New List <employee> (Size ); 15 } 16 }
We can see that this will affectProgramAnd create unnecessary objects. Therefore, if you need to execute different initialization methods in different constructors, the correct method should not apply to the initiatator, but declare the variable first, then initialize the member variables in the constructor as follows:
1 Class Employee 2 { 3 // Declare Variables 4 Private List <employee> Emplist; 5 6 Public Employee () 7 { 8 Emplist = New List <employee> (); 9 } 10 11 Public Employee ( Int Size) 12 { 13 Emplist = New List <employee> (Size ); 14 } 15 }
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.