1. the reference type and value type
are lightweight and the reference type is 'heavyweight.
the value type stores the object itself, while the reference type stores the memory address of the object
1. memory Allocation
value-type instances generally are allocated on the thread stack, instances of the reference type are allocated on managed heap .
Note: A value-type instance is not necessarily on the thread stack and has a relationship with the defined position. Local variables and method parameters must be on the stack, however, the member variables of a class are always allocated with other data in the class, which may be allocated to the stack.
C # is different from C/C ++, C # is based on.. NET environment, called hosting. I didn't understand it because I learned C language.
for example, variables stored in the heap or stack in C language are not determined by the variable type. Instead, variables are defined. The stack stores the value of local variables, and the system recycles memory allocation. The heap is created and recycled by a programmer.
in C # managed code, the memory storage location of variables is not based on the definition of variables: # ff6666 "> C # The variable type is incorrect, and the object instance is more precise. ) the type is determined.
stack and stack differences between C/C ++
click to open the link
click to open the link.
2. the Net Framework SDK documentation specifies the reference type and value type.
value type: the value type is called structure or enumeration. Struct, int32, Boolean, decimal, and these structures are derived from system. valuetype. All enumerations are from system. derived from Enum
reference type: All classes are reference types
3. initialization
when a value type variable is created, it is a value containing the basic type by default, all members are initialized to 0
when a reference type variable is created, the default value is null, indicating that no valid object is pointed, an nullreferenceexception is thrown.
C # value type and reference type