1. initialize the device (inWhen declaring a fieldInitialization, rather than processing in constructor)
Initialize the generatedCodeIt is placed before the Type constructor. Initialization is executed before the constructors of the base classes of the execution type, and they are executed in the order of the declared order.
2. Under what circumstances should variables not be initialized?
First, if you initialize an object as 0 or null. By defaultAll content is initialized to 0. The initialization of system 0 is based on the underlying CPU instructions, and the entire memory block is set. Any other initialization statement that sets 0 is redundant. C # The Compiler faithfully adds additional commands to set the memory to 0 (Additional settings). There is nothing wrong with this, but the efficiency is not high (Binning and unboxing). In fact, it is not worthwhile to process value-type data.
The second inefficiency occurs when you add two constructors to an object.You use the initializer to initialize variables, and all constructors initialize these variables.
Public class myclass
{
Private arraylist _ coll = new arraylist ();
Myclass ()
{
}
Myclass (INT size)
{
_ Coll = new arraylist (size );
}
}
The above code will be compiled into the following code by the compiler
Public class myclass
{
Private arraylist _ Coll;
Myclass ()
{
_ Coll = new arraylist ();
}
Myclass (INT size)
{
_ Coll = new arraylist ();
_ Coll = new arraylist (size );
}
}
When you create a new myclass object and specify the size of the set, you actually create two arraylist arrays. One of them quickly becomes a spam object. The initialization tool is executed before all constructors, so that it creates an arraylist. Then, the constructor creates 2nd arraylist arrays.
3. You should know that you should initialize its static member variables before any instance of the type is initialized. In C #, you can use static initializes and static constructor to achieve this purpose. A static constructor of a class is distinctive. It is executed before all methods, variables, or attributes are accessed. You can use this function to initialize static member variables, force the single-piece mode, or implement any other work that should be done before the available type of instances.
Similarly, like the initialization tool of the instance, the static initialization tool is executed before the static constructor is called. In addition, your static initiator is executed before the static constructor of the base class is executed.
4. Construct the Operation Sequence of the First Instance Object
1. Set all static variables to 0.
2. Execute the static variable initialization tool.
3. Execute the static constructor of the base class.
4. Execute the static constructor of the current type.
5. Set all instance variables to 0.
6. Execute the instance variable initiator.
7. Execute the appropriate base class instance constructor.
8. Execute the current type of instance constructor.
Subsequent instances of the same type start from step 1, because the class initiator only runs once. In addition, steps 6th and 7th can be optimized and placed in the constructor initialization so that the compiler can remove repeated commands.