I. C # Mean Value Type and reference type
1. Class class reference type, structure struct Value Type
2. array is a reference type. Even if the element is a value type, int [] is a reference type.
3. enumeration is a value type Enum
4. The delegate type delegate is the reference type.
5. The interface type is a reference type, but can be implemented by the value type.
II. value expression: the value of the expression "2 + 3" is 5. For an expression of the reference type, its value is a reference, rather than the object referred to by the reference, such as string. the empty value is not an empty string, but a reference to the empty string.
III. The value of a variable is stored in the declared position. The value of a local variable is always stored in the stack, the instance of the referenced object is always stored in the heap, and the static variable is also stored.
V. the value type cannot be derived from other types. No additional information is required to describe the actual type of the value. The reference type starts with a data block that identifies the actual type of the object, other information is also provided. Never change the object type. When a simple forced type conversion is executed, a reference is obtained during the runtime to check whether the referenced object is a valid object of the target type, if the original reference is returned, an exception is thrown.
Misunderstanding:
Misunderstanding 1: The structure is a lightweight class
Misunderstanding 2: The reference type is stored on the stack, and the value type is stored on the stack.
The referenced instance is always created on the stack. However, a class has an int instance variable. In any object of the class, the value of this variable is always associated with other data of the object, that is, on the stack. Only local variables (variables declared inside the method) and method parameters are on the stack. For C #2.0 and later, many local variables are not fully stored in the stack. For details, refer to the subsequent anonymous method.
Misunderstanding 3: objects are passed by reference in C # by default. When the reference type is passed as a method parameter, "value transfer" is used by default. This value is a reference.
Vi. unpacking and packing
For a variable of the reference type, its value is always a reference; for a variable of the value type, its value is always a value of the value type.
int i=5;object o=i;int j= (int)o;
I is a value type variable, and O is a reference type variable. Assign I to O for packing: an object with a value (5) will be created on the heap during runtime. The O value is a reference of this object. The value of this object is a copy of the original value. Changing the I value has no effect on the value of this object.
Unpacking: O is split into int type values, and the unpacking is correct (that is, the O value can be converted to int). Copy the value in the box to J, after unpacking, J has nothing to do with this object.
It is easy to see that there is a forced type conversion. When will it be packed? The packing is relatively concealed. Generally, the tostring, equals, or gethashcode method is called for a value type. If this type does not overwrite these methods, it is boxed. GetType () cannot be overloaded. It is boxed when the value type is called, so it can be replaced by typeof. In addition, when a value type is assigned to the interface variable, it is also boxed, such as icomparable x = 5;