I really love it. Today, let's gossip about the C # constructor:
(1) Let's first take a look at the instance constructor of the reference type ):
Test 1, without parameters:
As long as they are programmers, they all write code like this. We can even omit the keyless ctor of B and A. However, in CLR, the default keyless Ctor is created for B and A by default ), when new B is used, the class goes from descendant to ancestor until the base class of all classes: Object ctor.
When we create a form in Visual Studio, we are used to the following code:
public partial class Form1 : Form{ public Form1() { InitializeComponent(); }}
Habits are the most terrible. It will make you lose yourself, don't believe it? See the following test:
Test 2: change the "No parameter ctor" in "B" to "with parameter ctor:
Compilation fails, as shown in the error. Someone said, isn't B a default constructor? Russia, it seems, can only be used inside CLR. We want to use it.
If you want to create the above B object, you must write it like this:
int v = 10;B b = new B(v);
Someone may ask why the ctor of B is not written:
That is to say, why not use base. I don't need to. Because there is only one coder in a at this time, no matter whether base is added to B or not, the coder of A will be called.
Test 3. If a has two cuts:
At this time, the ctor of B will still call the default no-argument ctor of A. If you don't believe it, try debug.
If ctor of ctor B has parameters at this time, it will still call the default ctor without parameters of a. Who told you not to add base to ctor B:
Finally, we will introduce the most formal practices:
In the end, the default constructor is useless because it is a rule developed by C # team. If an error is accidentally written, the first constructor will report an error, therefore, if you are not afraid of mistakes at work, you will be killed during the interview.
(2) then spoof the value-type instance constructor. To be honest, I am bored with it. It is better than the static class because I have never customized struct in the project, but every time I sum up the C # syntax, IT and static are special cases. However, I often write static classes in Silverlight commands.
Test 1:
The result is that all the members of struct are 0 or null. But do we really need such an object?
Therefore, you need to manually set the attributes in struct:
SomeValueType b = new SomeValueType() { y = 2 };b.x = 1;
Wahaha, struct actually supports the new C #3.0 syntax.
In addition, you will also find that struct can also have members of the reference type. Have you ever seen such a problem? I have never seen any project in five years.
Test 2:
If you want to use struct's no-argument ctor, never declare No-argument ctor in struct. If you don't believe it, try it, as shown below:
Test 3:
It seems that it is more reliable to make a ctor with parameters:
However, all the variables in struct must be initialized in the parameter ctor. If you don't believe that, you will be killed if you have only one variable, as shown below:
Test 4:
If you dare to write this:
This is equivalent to failing to understand my article. Please test Goto.
(3) Finally, yy the class ctor. I used to call it cctor.
Only one cctor with no parameter exists and cannot carry an access modifier (for example, private). The default value is private (but you are not allowed to specify private ):
To put it bluntly, cctor is used to instantiate the static members in the class. It is a one-time task. It is executed only when the class is created (new), instead of when the class is declared.
In the above example, we will find that A is equal to 1 first and then 3, which means static inline assignment is earlier than cctor.
In other words, cctor can be used to reference type/value type, but defining cctor in value type is meaningless. After reading the above, we will know that the value type has no chance to execute cctor.
The cctor call process is as follows:
During compilation, the JIT compiler checks whether the appdomain executes cctor (recorded) to determine whether to generate the call code;
If multiple threads exist during execution, a mutex thread synchronization lock is required to ensure that only one cctor is executed.
Cctor is located in appdomain, so static finalize () methods are not supported, that is, GC-immune.
The cctor does not call the cctor of its base class. There is no relationship between the two.
Originally, cctor is very simple, but it is easy to confuse when it is mixed with "instances.
Static variables and instance variables are not the opposite.
1) You can use static members/methods in the instance method.
2) The static method cannot use instance members defined outside the static method.
3) In static methods, you can use instance members defined in the static method.
4) permanent exception -- Main Function
class A{ private static int a = 1; static A() { a = 3; } public string b = "bjq"; public A() { a = 2; } static void Main(string[] args) { Console.WriteLine(a);
}}