C # differences between the value type and the reference type
C # differences between the value type and the reference type
In C #, there are two types of variables: Value Type and reference type.
Value-type variables directly store values. More specifically, value-type variables directly store their own values in the memory, as shown in the code,
Int x = 2;
The value type variable x directly stores its own value somewhere in the memory: 2.
Variables of the reference type store references to their objects. Furthermore, variables of the reference type directly store a reference pointing to it in the memory, the referenced object is stored in the memory of the referenced type variable. As shown in the code,
Public class Thing
{
Int x;
}
Thing x = newThing ();
When referencing the type variable x, when declaring it, it will allocate a space somewhere in the memory to store its own content, and the variable x we use, in fact, it is a reference pointing to its own content.
Note: Later we will talk about the reference type variable, whose content is always stored in the heap; while the value type variable, its own values are always allocated to the place where it is declared (either in the stack or in the heap ).
So what are the differences between the value type and the reference type in C #? Let's list them.
1. Because a Value Type Variable stores its own value, it always contains a value and cannot be empty. A reference type variable stores a reference pointing to it, therefore, it can be null, indicating that the value is not included, that is, it does not point to any place in the memory.
2. The reference type is directly inherited from the System. Object Class. Different from this, the value type is directly inherited from the System. ValueType class, while the System. ValueType class is directly inherited from the System. Object Class. As a base class of all types, the System. Object class provides a set of methods that can be found in all types, such as the ToString method. The System. ValueType class inherits the System. Object class. It does not add any new members, but only overwrites some inherited methods to make them more suitable for value types. Note: You can use the Type. IsValueType attribute to determine whether a Type is a value Type, as shown in the code.
Thing x = new Thing ();
If (x. GetType (). IsValueType ){
// Todo Something
}
3. reference type objects and value type objects. They have different storage allocation rules in the memory. The reference type object is always allocated to the heap, while the value type object is always allocated to the declared place. If the value type object is declared as a field, it will be allocated along with the object to which it belongs. If the value type object is declared as a local variable, it will be stored on the stack. As shown in the code,
Public class Thing
{
Int x; // x is used as a field, and x is a value type. Objects following the reference type are allocated and located on the heap.
}
Thing x = new Thing ();
Int x = 2; // x is a local variable. x is a value type and is located on the stack.
4. All value types are seal and cannot be derived from new types. The reference type can be further derived. This is also why the value type is more suitable for data storage, and the reference type is more suitable for defining behavior.
Finally, we will list the division of the C # value type and the reference type as follows:
Data Type as Value Type |
Bool byte char decimal double float int long short sbyte uint ulong ushort enum struct |
Data Type as reference type |
Class object string interface delegate |
Well, the difference between the C # value type and the reference type is recorded so much for the time being, and will be improved if supplemented in the future.