Static members and static constructor in the structure of. Net/C #

Source: Internet
Author: User

In the. NET/C # structure data type,

The Global static member variables can be defined and referenced directly by the structure name. This is no different from the usage of the class,

However, when using a static constructor, consider the following code:

 

  1 using System;
2  using System.Collections.Generic;
3  using System.Linq;
4  using System.Text;
5
6  namespace TestProjectR
7 {
8 struct S1
9 {
10 public int i;
11 static S1()
12 {
13 Console.WriteLine("TEST!!!");
14 }
15 }
16
17 class Program
18 {
19 static void Main(string[] args)
20 {
21 S1 s = new S1();
22 s.i = 10;
23 Console.WriteLine(s.i);
24 }
25 }
26 }

The actual output is not:

 

 

Test !!!

10

 

Instead, only the second row and 10 rows are output, because the static constructor is not called at all ......

 

I think it may be because there is no static member variable in the structure, and there is no relevant operation in the static constructor, so it is not executed, so the change example is as follows:

 

 

  1 using System;
2  using System.Collections.Generic;
3  using System.Linq;
4  using System.Text;
5
6 namespace TestProjectR
7 {
8 struct S1
9 {
10 public static int a;
11 public int i;
12 static S1()
13 {
14 a = 5;
15 Console.WriteLine("TEST!!!");
16 }
17 }
18
19 class Program
20 {
21 static void Main(string[] args)
22 {
23 S1 s = new S1();
24 s.i = 10;
25 Console.WriteLine(s.i);
26 }
27 }
28 }

 

The result output is still 10, and the static constructor is still not called.

 

Well, it may be because the static member variable is not used, so the static constructor is not called, so the sample code is modified again as follows:

 

 

  1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace TestProjectR
7 {
8 struct S1
9 {
10 public static int a;
11 public int i;
12 static S1()
13 {
14 a = 5;
15 Console.WriteLine("TEST!!!");
16 }
17 }
18
19 class Program
20 {
21 static void Main(string[] args)
22 {
23 S1 s = new S1();
24 s.i = 10;
25 Console.WriteLine(s.i);
26 Console.WriteLine(S1.a);
27 }
28 }
29 }
30

OK. The output is as expected this time:

 

 

Compared with a class, a class is different. As long as an object is created for a class, its static constructor will be called, regardless of whether the static member variable is operated or not.

 

However, there is another problem: the structure is a value type, and its initialization allocation is generally allocated in the stack as shown above, when a schema instance exits the scope, the memory is automatically released from the stack.

 

But what about static fields in the structure? How is it allocated? If it is a class, the static fields of the class are stored in two completely different locations from the instance fields. Although they are all in the heap, they can be allocated to different areas in the heap,

Can stack operate like this? Questions are retained here !!!!!!

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.