Talking about the value type and reference type in C,
In C #, the value type and the reference type are two important concepts. The behavior of the type instance must be determined when designing the type. If you cannot understand the differences between the reference type and the value type when writing code, it will cause unnecessary exceptions to the code. Many people have encountered many problems in programming because they have not figured out these two concepts. Here, the blogger will talk about the understanding of value and reference types.
First, conceptually, the value type directly stores its value, while the reference type stores its reference to its value. The two types are stored in different places in the memory.
Second, from the perspective of memory space, the value type is in the stack, and the reference type is allocated to the heap.
The stack allocates memory space during compilation, which is clearly defined in the Code, and the heap is the memory space dynamically allocated in the program running, the memory size can be dynamically allocated based on the program running status. Therefore, the value type always occupies a predefined number of bytes in the memory. Variables of the reference type allocate a memory space in the stack. The memory space contains references to another memory location, which is an address in the managed heap, that is, the place where the actual value of this variable is stored.
That is to say, the value type is equivalent to cash, and it is used directly when used, and the cited type is equivalent to passbook. It must be obtained first by the bank.
However, the value type allocates memory on the stack, while the reference type allocates memory on the managed stack. And then describe it in detail.
(1) For a value-type instance, if it is a local variable in the method, it is created on the thread stack. If the instance is a member of the type, it is part of the type member, other types of fields are stored on the managed stack.
Each value type has an implicit default constructor to initialize the default value of this type. For example:
int i = new int();
It is equivalent:
Int32 i = new Int32();
It is equivalent:
int i = 0;
It is equivalent:
Int32 i = 0;
When the new operator is used, the system calls the default constructor of a specific type and assigns the default value to the variable. In the preceding example, the default constructor assigns the value 0 to I.
Note: All value types of C # are implicitly derived from System. ValueType, while System. ValueType is directly derived from System. Object. That is, System. ValueType is a class type rather than a value type. The key is that ValueType overrides the Equals method to compare the value type by instance value rather than the reference address.
(2) the instance of the reference type is created on the managed stack.
The following code details the differences between the value type and the reference type.
1 namespace Test 2 {3 class Program 4 {5 static void Main (string [] args) 6 {7 // call the Demonstration method 8 ReferenceAndValue in the ReferenceAndValue class. demonstration (); 9 Console. readLine (); 10} 11} 12 public class stamp // defines a class 13 {14 public string Name {get; set ;} // define the reference type 15 public int Age {get; set ;} // define value type 16} 17 public static class ReferenceAndValue // define a static class 18 {19 public static void Demonstration () // define a static method 20 {21 stamp Stamp_1 = new stamp {Name = "Premiere", Age = 25 }; // instantiate 22 stamp Stamp_2 = new stamp {Name = "Again", Age = 47}; // instantiate 23 int age = Stamp_1.Age; // obtain the value of the Value Type Age 24 Stamp_1.Age = 22; // modify the value of the Value Type 25 stamp guru = Stamp_2; // obtain the value 26 Stamp_2.Name = "Again Amend" in Stamp_2 "; // modify the referenced Name value 27 Console. writeLine ("Stamp_1's age: {0}", Stamp_1.Age); // display the Age value in Stamp_1 28 Console. writeLine ("age's value: {0}", age); // displays the age value 29 Console. writeLine ("Stamp_2's name: {0}", Stamp_2.Name); // display the Name value of Stamp_2 in 30 Console. writeLine ("guru's name: {0}", guru. name); // display the Name value 31} 32} 33} In guru}
After running the above program, we can see that when the value of Stamp_1.Age is changed, the age is not changed, but the anders is changed. after the value of Name, guru. the Name is changed, which is the difference between the value type and the reference type. When declaring the age value type variable, the value of Stamp_1.Age is assigned to it. At this time, the compiler allocates a space on the stack and then fills in the value of Stamp_1.Age. There is no association between the two, just like copying a file in a computer, I just copied the value of Stamp_1.Age to age. The reference type is different. Stamp_2 is assigned to the guru when the guru is declared. As mentioned above, the reference type only contains the reference of the Data region address on the stack, in fact, the reference of Stamp_2 is also assigned to guru, so they point to the same memory area. Since it is pointing to the same region, no matter who you change, the other value will change, just like a credit card and a family card, using a family card to get money, the associated credit card accounts also change.
Author: xin, published in blog
Reprinted please indicate the source, welcome to mail exchange: zhuanxinxin@foxmail.com