Remember a year ago when I wrote the code, I asked the little buddy a question, and I said, "You say, if I initialize it at the time of the declaration, what stage of the object initialization will it be assigned to?" "The Little buddy thought to answer me:" Or in the constructor of the variable initialization. "The program when the mouth, time is relatively tight, there is no space to verify the problem, and then slowly forgotten."
This holiday opened "C # Nature Theory" Just inside also mentioned this situation, although only a stroke, but also reminds me of memories, that today to record this question and its extension of some of the knowledge points.
1. General constructor function
public class Constructiontest
{
int intpro = 1;
Public Constructiontest ()
{
}
}
When an instance field is assigned a value at the time of declaration, the location of the assignment is moved when the compiler declares it at processing time, and becomes the first statement in the constructor. The code is similar to the following code (if you are interested in viewing il after compiling):
public class Constructiontest
{
int Intpro;
Public Constructiontest ()
{
Intpro = 1;
}
}
So what happens if a variable is assigned at the time of Declaration and is assigned a value in the constructor? The answer is: the assignment in the constructor overrides the value initialized at the time of the Declaration. The code is similar to the following code:
public class Constructiontest
{
int Intpro;
public constructiontest (int value)
{
Intpro = 1;//declared when the value is assigned
Intpro = Assignment in value;//constructor
}
}
Therefore, it is best to initialize the fields in the constructor, especially in the case of team collaboration development, it is important for complex projects to follow the same development specifications and guidelines.
2. Static constructors
Static constructors each of the relevant data in C # describes one side of its characteristics, such as: Each class can have only a single, declaration without the visibility of the modifier, must be decorated with static, and does not allow parameters. Of course, like a normal constructor, its initialization within a constructor overrides the content it initializes when it declares.
So here are some points to note: 1) static constructor invocation features: Not a display call, but a ' runtime ' is automatically called when the class is first accessed, and is only called once. The so-called ' first-time access ' may be to invoke a common constructor or to access a static method or field of a class. It is because of this characteristic of static constructors that we can use it to solve the problem of creating instances in multi-threaded cases in singleton mode. The specific code will be shown below. 2) Suggestions for static initialization: (original) "To complete static initialization at the same time as the declaration." A static constructor is executed before any member of the class is first accessed, whether the member is a static field or a static method, or another constructor. To provide support for this behavior, the compiler automatically inserts some code that is responsible for checking. The code checks all types of static members and constructors to ensure that the static constructor runs first. However, without a static constructor, the compiler initializes all static members to their default values and does not add checks for any static constructors. This results in the assignment and initialization of static members as long as any static fields are accessed. However, it is not said that the static method and the instance constructor can be called only if the assignment and initialization have been completed. In some cases, the cost of initializing a static member may be higher, using the design above, unless you want to access a static field, you do not need to initialize it. However, initializing static members can improve performance to some extent. "The advice is very pertinent, and the most striking part of C # essentialism is the so-called" advanced themes "of these paragraphs.
Using the characteristics of static variables, the singleton mode of static constructor is realized:
Public sealed class Singleton {private static readonly Singleton instance; Private Singleton () {}
static Singleton ()
{
instance = new Singleton ();
} public static Singleton Instance {get {return Instance; } } }
Singleton mode should be the most basic and familiar with the design pattern, and another kind of lock implementation is very simple, we can refer to the link: https://msdn.microsoft.com/zh-cn/library/ff650316.aspx
The constructor is done here, and then there is a little bit of the new operator. Many people have explained the new operator's creation of instances, such as ' you must know. NET ' that explains the process very carefully, and no one has ever seen a friend suggest a blog to look at the garden, of course, it is best to buy a book slowly taste.
But after I saw the description in the ' C # essence ', I could not help but sigh and write it thoroughly. Although not so ' deep ' (no IL is listed), the understatement clearly outlines the process of interacting between the new operator and the constructor: The new operator obtains memory from the memory manager, and then invokes the established constructor, passing the initialized memory to the constructor. Then the remainder of the constructor chain starts to execute,
pass the initialized memory between the constructors. None of these constructors return a value (both return void), and after execution on the constructor chain is finished, the new operator returns the memory drink, which points to the memory in the form of a good initialization.
the content of the constructor here, there are some things I am interested in will be sorted out, some of the questions may also be discussed with the small partners, no matter how to write a blog is really manual work, and the format is still a little annoying. To those who have written a lot of colleagues to salute, thank you for sharing.
Talking about---C # essence on reading notes from the constructor function