The type of the data item defines the amount of memory that is required to store the data, the data members that make up the type, and the functions that the type can perform. The type also determines where the object is stored in memory-the stack or heap.
Types are divided into: value types and reference types, and the two types of objects are stored in different ways in memory. Value types require only a single piece of memory to store the actual data, and the reference type requires two segments of memory: The first segment stores the actual data, it is always in the heap, the second is a reference (that is, the memory address, does not know if it is direct addressing), and points to where the data resides in the heap.
If the data is not a member of another type, it is independent, such as the temporary variable int age that the method code block declares, and if it is a member of a type, such as Class person{private int age;public int age{get{rerurn age;} Set{age=value;}}, for the first case the data is stored as: For value types, the data is stored in the stack, and if it is a reference type, the actual data is stored in the heap, and the reference (the memory address pointing to the actual data) resides in the stack. However, in the second case, the object of the reference type is always stored in the heap, the value type object, or the reference part of the reference type data can be stored in the heap, or stored in the stack, depending on the actual environment, the member of the above person class age,age is int, although it is a value type, However, it is part of the person instance data and is therefore placed in the heap along with the object's data.
For any object of a reference type, all its data members are stored in the heap, whether it is a value type or a reference type.
The following code verifies that the value type and reference type are stored differently:
Public classValuechange_forward {Private intNumone; Public intNumone {Get{returnNumone;} Set{Numone =value;} } } Public structValue_keep {Private intNumtwo; Public intNumtwo {Get{returnNumtwo;} Set{Numtwo =value;} } Static voidMain (string[] args) {Valuechange_forward Demo_forward=NewValuechange_forward (); Demo_forward. Numone=8888; Valuechange_forward Test_foward=Demo_forward; Test_foward. Numone=666; Value_keep Demo_keep=NewValue_keep (); Demo_keep. Numtwo=555; Value_keep Test_keep=Demo_keep; Demo_keep. Numtwo=222; Console.WriteLine ("Reference type: Custom Class Valuechange_forward, instance Demo_forward. numone=8888, make Valuechange_forward test_foward=demo_forward; Test_foward. numone=666; Demo_forward after assigning value. Numone={0},test_forward={1}", Demo_forward. Numone, Test_foward. Numone); Console.WriteLine ("value type: Custom Structvalue_keep, instance demo_keep. numtwo=555, make Value_keep test_keep=demo_keep;demo_keep. Numtwo = 222; Demo_keep after assignment. Numtwo={0},test_keep={1}", Demo_keep. Numtwo, Test_keep. Numtwo); Console.ReadLine (); }
Difference between a value type and a reference type