In C #,
value type: Integral type, Boolean type, character type, real number type, structure type, enumerated type.
Reference type: Class, object, string, array, interface, delegate.
difference:
1, the value type is usually allocated on the stack, its variable directly contains a variable instance, the use of high efficiency.
2. A reference type is assigned to a managed heap, and a variable of a reference type typically contains a pointer to an instance through which the variable references the instance.
3, the value type inherits from ValueType (note: While System.ValueType inherits from System.Object), and the reference type inherits from System.Object.
4, the value type variable contains its instance data, each variable holds its own copy of the data (a copy), so by default, the parameter passing of the value type does not affect the parameter itself, and the reference type variable holds the reference address of its data, so passing the argument by reference affects the parameter itself. Because two variables refer to the same address in memory.
5, the value type has two kinds of representations: boxing and unboxing; The reference type has only one form of boxing. I will discuss this topic in depth in the next section in a dedicated space.
6, the typical value type is: Struct,enum and a large number of built-in value types, and can be called a class can be said to be reference types.
7. Memory of value type is not controlled by GC (garbage collection, Gabage collection), when the scope ends, value type releases itself, reduces the pressure of the managed heap, therefore has the advantage of performance. For example, struct is usually more efficient than class, and reference-type memory recycling is done by GC, and Microsoft even advises users not to free up their own memory.
8. Value types are sealed (sealed), so value types cannot be base classes of any other type, but can be inherited or multiple-inherited interfaces, and reference types are generally inherited.
9, the value type does not have polymorphism, and the reference type has polymorphism.
10. Value type variables cannot be null values, value types are initialized to 0 values themselves, and reference type variables are created by default as null values, indicating that there is no reference address pointing to any managed heap. Any operation of a reference type with a value of NULL throws a NullReferenceException exception.
11. There are two states of value types: Boxed and unboxed, and the runtime provides a boxed form of all value types, whereas a reference type usually has only one form: boxing.