[C # tips] Interesting Type Static Constructor

Source: Internet
Author: User

This is an interesting phenomenon in C #. You may be able to see the behavior of CLR in constructing types and the trigger compilation process of JIT compilation.

See the following code:

1 class Program
2 {
3 static void main ()
4 {
5 myvaluetype1 type1 = new myvaluetype1 ();
6 console. writeline (myvaluetype1.myint );
7 console. writeline ("**********************");
8 myvaluetype2 type2 = new myvaluetype2 ();
9 type2.myint = 123;
10 console. writeline (type2.myint );
11 console. writeline ("**********************");
12 myvaluetype3 type3 = new myvaluetype3 ();
13}
14}
15
16 struct myvaluetype1
17 {
18 static myvaluetype1 ()
19 {
20 console. writeline ("hello from myvaluetype1 ");
21 // Myint = 111;
22}
23 public static int32 Myint;
24}
25
26 struct myvaluetype2
27 {
28 static myvaluetype2 ()
29 {
30 console. writeline ("hello from myvaluetype2 ");
31}
32 public int32 Myint;
33}
34
35 struct myvaluetype3
36 {
37 static myvaluetype3 ()
38 {
39 console. writeline ("hello from myvaluetype3 ");
40 Myint = 333;
41}
42 public static int32 Myint;
43}

Three structures are defined: myvaluetype1, myvaluetype2, and myvaluetype3. The three structures contain a static constructor and each constructor has a code for output. In myvaluetype1 and myvaluetype3 static. Then we create three new instances in the main function. You can first think about the output result.
In fact, you will get the following results:


We can see that although all three structures have static constructors, only the first structure is executed. In fact, this interesting phenomenon is also the CLR's consideration of performance. Unless the type is accessed, it will never be called to its Type constructor. This process is JIT.
When the sixth line of code is executed, CLR tries to go to myvaluetype1 to find the value of the static field Myint. At this time, myvaluetype1 is actually accessed. The static constructor is executed to obtain the corresponding output.
In myvaluetype2, Myint is an instance member and its access value is only related to the instance type2. It has nothing to do with the type itself, and CLR will not execute the static constructor of the type myvaluetype2.
Myvaluetype3 is almost the same as myvaluetype11, and Myint is a static member. However, in the main function, myvaluetype3 is not actually accessed, but uses it to construct a virtual object structure, all fields in this object structure are assigned a value of 0 or null.
Therefore, the output in the second row is zero.
These properties are inseparable from the JIT compiler.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.