Differences between C # value types and reference types
1. Data of value type is stored in the stack of memory, the data of the reference type is stored in the heap of memory, and only the address of the object in the heap is stored in the memory unit.
2. Value type access speed is fast, reference type access speed is slow.
3. Value types represent actual data, and reference types represent pointers or references to data stored in the memory heap
4. Value types inherit from System.ValueType, reference types inherit from System.Object
5. The memory allocation of the stack is automatically released, while the heap is in. NET, there will be a GC to release
6. A variable of value type directly holds the actual data, whereas a variable of the reference type holds the address of the data, that is, the object's reference.
7. A value type variable stores the value of a variable directly on the stack, and a variable of the reference type holds the address of the actual data on the stack, while the actual data is stored in the heap.
Note that heap and stack are two different concepts, where storage is not the same in memory, heaps are typically used to store variable-length data, such as string types, while stacks are used to store fixed-length data, such as Integer type data int (each int variable occupies four bytes). Where the data is stored, it is known that when assigning a value variable to another value variable, two identical values are saved on the stack, and a reference variable is assigned to another reference variable, and two references to the same heap location are saved on the stack, that is, the address of the same heap is saved on the stack. In the case of data manipulation, for value types, because each variable has its own value, the operation of one variable does not affect other variables, and for a variable of reference type, manipulating the data of a variable is the operation of the data in the heap, if a variable of two reference types refers to the same object, The actual meaning is that they hold the same address of the heap on the stack, so manipulating one variable affects another variable that references the same object.
Increase understanding
Differences between C # value types and reference types