New is a frequently used keyword.
Here we only describe the role of the new object () type.
Others are not involved at the moment,
The content in this article is also relatively simple, and some common procedures are listed.
1. When accessing an assembly for the first time (the corresponding program will be loaded, for example, the new object needs to be in another dll)
In fact, there are a lot of other content in step 1, but I will not describe it in detail here, because it is rarely used.
If the dll cannot be found, an exception is thrown.
2. assign values to all static members (static)
The assignment order in c # looks exactly the same as the code order from top to bottom, but it is not guaranteed that the same will be true for later. net versions and compilers. Therefore, do not rely on the code order.
3. Call the static Constructor (... ctor)
The static constructor is called only once from start to end. If you want to create the singleton mode, it is also OK to initialize it here,
If a static constructor throws an exception, all the members of this class will not be able to access the static constructor during the application's lifecycle. Upon access, the System. TypeInitializationException will be thrown.
Therefore, exercise caution in the static constructor, preferably try catch, if not a very serious error.
The Order relationships mentioned in point 2nd can be executed sequentially in the constructor.
4. if this class has a parent class, call the constructor of the parent class first. (if the parent class has a parent class, so continue to call the parent class of the parent class .......)
All the way to the highest level, if you want to call a specific constructor of the parent class, you can use base (.....)
5. Initialize all instance members
6. If this (...) exists, it means to call other constructors first.
7. start calling the constructor.
Generally, constructors should be lightweight as much as possible,
A lot of details are ignored here, such as what is done in the static Constructor (that is, the Type constructor)
PS1: The value type can also be int I = new int (); but actually it is no different from int I = 0. of course, the value type you define can be used to create some useful constructors.
PS2: A lot of memory allocation is also omitted here