Thinking caused by an interview question (C# value type and reference type)

A certain month in a certain year, the author went to interview an outsourcing project of China Merchants Bank. After I came to the interview site, the interviewer gave me a test paper. The test paper only had two questions, one of which was as follows:

Read the program below

 1  class Program
 2     {
 3         struct Point
 4         {
 5            public int x;
 6            public int y;
 7             public Point(int x,int y)
 8             {9
 9                 this.x = x;
10                 this.y = y;
11             }
12         }
13 
14  
15         static void Main(string[] args)
16         {
17             Point p1 = new Point(100,100);
18             Point p2 = p1;
19             p1.x = 200;
20             Console.WriteLine("{0},{1}", p1.x, p2.x);
21             Console.ReadLine();
22 
23         }
24  }

Please write the values ​​of p1.x and p2.x.

   The author belongs to the kind of code farmer who is not very solid in foundation. He is usually a CURD Boy. Although he understands that this question is to investigate the allocation of variables in memory, but I didn’t pay much attention to this problem before, and I couldn’t answer it for a while. come up.

  In fact, this topic only needs to understand that the Struct structure is a variable of value type. The so-called value type, conceptually speaking, is to directly store the value of the variable. Then the sentence Point p2=p1 means to create an identical variable p2 in another place in the memory (stack). The values ​​of p2 and p1 are the same, but they are two completely different variables. Changing the value of p1 does not affect p2, so Finally p1.x=200, p2.x=100.

  If you change the struct in the title to class, the code becomes like this:

 

  class Program
    {
        class Point
        {
           public int x;
           public int y;
            public Point(int x,int y)
            {
                this.x = x;
                this.y = y;
            }
        }

 
        static void Main(string[] args)
        {
            Point p1 = new Point(100,100);
            Point p2 = p1;
            p1.x = 200;
            Console.WriteLine("{0},{1}", p1.x, p2.x);
            Console.ReadLine();

        }
 }

  What was the result?

 

   The answer is p1.x=200, p2.x=200. Because in C#, Class is a reference type, p1 and p2 actually point to the same memory area, and modifying the value pointed to by p1 will affect the value pointed to by p2. Kind of like pointers in C#.

   To sum up, the data types in C# are divided into two types: value types and reference types. A value type stores the value of a variable, and a reference type stores a reference to the variable. The two can be stored in different places, value types are stored on the stack, and reference types are stored on the managed heap. Regarding stack and managed heap, further study is required.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326135464&siteId=291194637