1. Value type (ValueType)
Value types include: numeric type, struct, bool, user-defined struct, enum, nullable type.
Variables of value types store data directly and are allocated in the managed stack. Variables are automatically released when the method that created them is returned, such as declaring a char-type variable name= ' C ' in a method, and the memory that the name variable occupies on the stack is automatically freed when the method that instantiates it ends
All value types in C # are implicitly derived from System.ValueType.
struct: struct (directly derived from System.ValueType).
Numeric type: integer, SByte (alias of System.SByte), Short (system.int16), int (System.Int32), Long (System.Int64), Byte (System.Byte), ushort (system.uint16), uint (System.UInt32), Ulon G (System.UInt64), char (System.Char).
Float type: float (system.single), double (system.double).
High-precision decimal type for financial calculations: decimal (System.Decimal).
BOOL Type: bool (alias of System.Boolean).
User-defined struct (derived from System.ValueType).
Enum: Enum (derived from System.Enum).
Nullable type (derived from system.nullable<t> generic struct, T? is actually an alias of system.nullable<t>
2. Reference type (referencetype)
Reference types include: array, user-defined class, interface, delegate, object, string, null type, class.
A variable of a reference type holds a reference to the data stored in the data heap, allocated in the managed heap, and the variables do not release memory at the end of the method that created them, and the memory they occupy is freed by the garbage collection mechanism in the CLR.
Array (derived from System.Array)
The user needs to define the following types:
Classes: Class (derived from System.Object);
Interface: Interface (interface is not a "thing", so there is no question of where to derive it.) The interface simply represents a contract convention [contract]).
Delegate: Delegate (derived from System.Delegate).
Object (alias of System.Object);
String: String (alias of System.String).
3. Value types differ from reference types:
|
Value type |
Reference type |
Storage mode |
Directly stores the data itself |
A reference to the data stored in the data heap |
Memory allocation |
Allocated in the stack |
Allocating in the heap |
Efficiency |
High efficiency, no address translation required |
Less efficient and requires address translation |
Memory Recycling |
Recycle immediately after use |
Do not recycle immediately after use, but instead to GC processing recycle |
Assignment operation |
Create a new object |
Create a reference |
Type extension |
Not extensible, all value types are sealed (seal), so new value types cannot be derived |
Multi-state features for easy expansion |
Instance Assignment |
is usually allocated on the thread stack (static allocation), but in some cases it can be stored in the heap |
Always allocated in the process heap (dynamically allocated) |
Value type and reference type tree structure:
Note: When the parameter is FEF (out), the argument is a reference pass, which passes the stack address (pointer, reference), otherwise the normal value is passed---the copy of the stack's original data.
4. Memory allocation
Instances of value types are often stored on the stack. But there are also special cases. If an instance of a class has a field of value type, the field is actually stored in the same place as the class instance, which is the heap. However, objects of reference types are always stored in the heap. If a struct's field is a reference type, only the reference itself is stored with the struct instance (on the stack or heap, depending on the case).
A reference type stores a reference in the stack whose actual storage location is in the managed heap. The abbreviation reference type is deployed on the managed push. A value type is always assigned where it is declared: as a field, it is stored as a variable (instance) to which it belongs, and is stored on the stack as a local variable. Value types are more efficient in memory management and do not support polymorphism, and are suitable for use as vectors for storing data; reference types support polymorphism and are suitable for defining the behavior of an application.
Note: A stack is a last-in-first-out data structure. In memory, variables are allocated on the stack to operate. A heap is an area of memory that allocates space for a type instance (object), creates an object on the heap, passes the address of the object to a variable on the stack (in turn, the variable points to the object, or the variable references the object).
5. Crating and Unpacking
1) Boxing is the conversion of a value type to an equivalent reference type
Allocates memory on the heap for a newly generated object that contains data that the object itself does not have a name for.
Copies the value of a value type variable on the stack to an object on the heap.
Returns the address of the object created on the heap to the reference type variable (the name of the variable is the same as the name of the object on the heap from the programmer's point of view).
2) Unpacking is the conversion of a reference type to an equivalent value type
Copy the values on the reference type variable heap onto the stack.
Summarize
After understanding the value types and reference types, we know that C # is a value pass, but some variables are reference types that require special attention when passing and copying. The method passes the parameter with ref (out) and passes the parameter for the reference.
Value passing only passes the value, without affecting the original value.
The reference is passed, the memory address is passed, and the memory address corresponding to the stored value is changed after the modification.
Value types and reference types