Home >Backend Development >C++ >What's the Key Difference Between Value Types and Reference Types in C#?
The key difference between the value type and the type of reference type in the c#
When processing data in C#, the difference between the understanding of the value type and the reference type is very important. This article explores their definitions and characteristics.
Value type (based on value)
Value type directly stores its data in the variables referenced. They include basic types, such as integer, floating point number and Boolean value. When you give the value to the value type, the copy of the value will be created.
Example:
int i = 5; int j = i;
and Quote type (based on reference) i
j
Example:
class MyClass { public int value; }; MyClass obj = new MyClass() { value = 5 }; MyClass obj2 = obj;
obj
obj2
The value type directly stores the data in the variable, and the address of the reference type storage data.
Value type Create a copy of the value, and the reference type creates a reference to the same object in memory. Change the values of the value in a variable will not affect other variables referenced, and change the value of the reference type with a variable can affect all reference variables.
The above is the detailed content of What's the Key Difference Between Value Types and Reference Types in C#?. For more information, please follow other related articles on the PHP Chinese website!