It can be understood as follows:
The Value Type stores specific values.
The reference type stores the value address.
For example
Int A = 1;
Int B =;
B ++;
At this time, B is 2, and A is still 1.
Let's look at the reference type again. For example, there is a user type
User U1 = new user () {name = "Zhang San "};
User U2 = U1;
U2.name = "Li Si ";
At this time, the names of U2 and U1 are Li Si, because when U2 is U1, u1 and U2 will point to the address in the same memory. Changing U2 is equivalent to changing u1,
C # differences between the value type and the reference type
1. Value-type data is stored in the memory stack; reference-type data is stored in the memory heap, while memory units only store
Address.
2. Fast value-type access and slow reference-type access.
3. The value type indicates the actual data, and the reference type indicates the pointer or reference to the data stored in the memory heap.
4. The value type inherits from system. valuetype, and the reference type inherits from system. object.
5. The stack memory allocation is automatically released, and the stack will be released by GC in. net.
6. variables of the value type directly store the actual data, while variables of the reference type store the data address, that is, object reference.
7. The Value Type Variable directly stores the value of the variable in the stack, and the reference type variable stores the actual data address in the stack, while the actual
The data is stored in the heap. Note that the heap and stack are two different concepts, with different storage locations in the memory. The heap is generally used for storage.
Variable-length data, such as the string type, while the stack is used to store data of fixed length, such as integer data int (each int variable
Occupies four bytes ). It can be learned from the data storage location that when one value variable is assigned to another value variable, two values are saved in the stack.
And assign a reference variable to another reference variable. The two references to the same heap location are saved in the stack.
That is, the address of the same heap is saved in the stack. During data operations, each variable has its own value type,
Therefore, operations on a variable will not affect other variables. For variables of the reference type, operations on the data of a variable are
Variables in the heap are operated. If two referenced variables reference the same object, the actual meaning is that they are saved in the stack.
The address of the heap to be stored is the same. Therefore, operations on a variable will affect another variable that references the same object.
Class person
{
Public int blood = 10;
}
Class Program
{
Public static void add (int x)
{
X + = 10;
Console. writeline ("value type after the parameter is passed and modified:" + x );
}
Public static void add (person)
{
Person. Blood + = 10;
Console. writeline ("reference type after the parameter is passed and modified:" + person. Blood );
}
Static void main (string [] ARGs)
{
// Value Type Variable
Int I = 10;
Console. writeline ("I Original Value:" + I );
Add (I );
Console. writeline ("but the I value is not modified due to function modification:" + I );
// Reference type variable
Person = new person ();
Console. writeline ("original blood value:" + person. Blood );
Add (person );
Console. writeline ("but the value of blood is modified due to function modification:" + person. Blood );
// The difference between the value type and the reference type is that when function parameters are passed.
// The Value Type copies its own value and passes it to other function operations. No matter how the copied value is changed, its own value will not change.
// The reference type is to pass its memory address to other function operations. The operation is to reference the type value itself. Therefore, the value is changed by the function.
// This is the difference between data transfer and data transfer.
Console. Readline ();
}
}
Difference between value type and reference type