In C#, the data types are divided intoValue TypeandReference TypeTwo types. Reference type variables store references to data, data is stored in the data heap, while value type variables store data directly. For reference types, two variables can refer to the same object. Therefore, operations on one variable may affect the object referenced by another variable. For value types, each variable has its own copy of the data, and the operation on one variable cannot affect another.
Value Type
All value types are inherited from the ValueType class, which better adapts to value types by overloading the virtual method of Object.
Although ValueType is an implicit base class of value types, classes inherited from ValueType cannot be created directly.
The value type includes the following two types:
- Structure Type, used to encapsulate data and related functions, including custom structures and built-in structures (such as: integer, floating point type, boolean, character type, and value tuple)
- Enum Type, defined by a set of named constants, representing an option or combination of options.
The structure type is inherited directly from,The enum type inherits from。
The value type can be empty,<T> (orT?) Generic types are represented, such as: int?, bool?. Therefore, <T> itself is also a value type.
public struct Nullable<T> where T : struct
AvailableStructConstraints to specify a value type whose type is a non-nullable value type (both structure type and enum type satisfy the constraint).
Value types cannot be inherited because all value types will eventually be compiled into sealed classes, but the structure can implement interfaces.
Here is a custom structure:
public struct Location { public double X; public double Y; public Location(double x, double y) => (X, Y) = (x, y); }
The value objects a and b with the same data are instantiated separately, and equality judgment is performed. The output result is as follows. From this we can see that the data itself is compared with the same value types.
var a = new Location(1,2); var b = new Location(1,2); var c = new Location(1,3); ((b)); // true ((c)); // false
Reference Type
The most common type of reference isclass,It also includes strings, arrays, delegates, interfaces, records, etc. All reference types are inherited from Object.
string: A special reference type that cannot be inherited and has immutability, but is more like a value type in usage.
string a = "123"; string b = a; a = "456"; (a); // "456" (b); // "123"
Record:Introduced in C# 9.0, it is not a new syntax, but a syntax sugar. Used to define a reference type that provides built-in encapsulation of data.
public record Person(string FirstName, string LastName);
Here is a custom class:
public class Location { public double X; public double Y; public Location(double x, double y) => (X, Y) = (x, y); }
Reference objects a and b with the same data are also instantiated separately, and equality judgment is performed. The output result is as follows. From this we can see that the reference types are equal to the reference address, not the data itself.
var a = new Location(1,2); var b = new Location(1,2); var c = b; (a == b); // false ((b)); // false (b == c); // true
Comparison of value types and reference types
1. The value type is stack-allocated or inline-allocated in the structure, and the reference type is heap-allocated.
2. The value type variable assignment copyes the object itself, while the reference type variable assignment copyes the object's reference.
3. Value types and reference types are ultimately inherited from Object.
4. Both structures and reference types in value types can implement interfaces.
5. Value types cannot be inherited because all value types are sealed, and reference types can derive new types (except string).
6. Value types have better efficiency in memory management and do not support polymorphism, and are suitable as carriers for storing data. Reference types support polymorphism and are suitable for defining the behavior of an application.
This is the end of this article about the use and comparison of C# value types and reference types. For more related C# value types, please search for my previous articles or continue browsing the related articles below. I hope you can support me in the future!