1: Stacks are stacks, because heaps and stacks are too awkward to say, like tongue twisters, so sometimes the stack is abbreviated as a stack . Heap and Stack, you see how comfortable this is. But whenever the stack is not equal to heaps and stacks, it must be said that heaps and stacks or heaps and stacks .
2: Value type variables and reference type variables are stored inside the stack. Whereas a value type variable holds the value of a value type instance, a reference type variable holds a reference to the instance of the reference type. A reference to an object is a pointer to an address in the heap that is saved in the heap, which is the object instance
3: usually an object itself contains an instance of a value type and an object, which distinguishes between the concepts of fields and variables. in general, it can be said that the declaration in a class is a field, which is declared in a method, is a variable. The field has an access modifier (public/private/protected/internal), and the variable does not.
4: The difference between a variable and a field , the field and the variable are similar, but in fact there is a big difference. The variable is always stored inside the stack, and the field follows the instance of the class.
5: The stack is a table that satisfies the last-in-first-out; the heap is a tree that satisfies the parent node's total not equal to (or not less than) the child nodes. not An object instance is a heap (a member of an object is a node), but all object instances are a heap (an object is just a node in the heap).
Value type: |
Int |
Float |
Char |
Double |
Structure |
............ |
Reference type: |
Class |
Interface |
Array |
String |
1: Data of value type is stored in the stack of memory and can be accessed quickly from the stack, so the value type represents the actual data.
2: A reference type represents a pointer or reference to data stored in the memory heap.
3: When data is released, value type data is automatically freed, and reference type data needs to be reclaimed by the garbage collector.
4: The default value of the value type is:0, the default value of the reference type is:null
for a class of value type, an instance of the class is stored in a variable, that is, an instance of the class follows the variable on the stack.
for classes of reference types, an instance of the class is stored on the managed heap.
C # Stack Tutorial