1. variables of the reference type only contain the memory address of the object. The memory address instead of the object itself will be copied, so modifications to the underlying object will be retained.
Unsafe class Program
{
Static void Main (string [] args)
{
Employee myE = new Employee () {id = 4 };
Fixed (int * pId = & (myE. id ))
{
Console. writeLine ("before method execution, the id address in myE is 0x {0: X} and the value is {1}", (uint) & (* pId), (uint) * pId );
}
TestMethod (myE );
Fixed (int * pId = & (myE. id ))
{
Console. writeLine ("after the method is executed, the id address in myE is 0x {0: X} and the value is {1}", (uint) & (* pId), (uint) * pId );
}
Console. ReadLine ();
}
Static void testMethod (Employee myE)
{
MyE. id = 5;
// MyE = new Employee () {id = 5 };
Fixed (int * pId = & (myE. id ))
{
Console. writeLine ("in method execution, the id address in myE is 0x {0: X}, and the value is {1}", (uint) & (* pId), (uint) * pId );
}
}
}
Output:
Before the method is executed, the id address in myE is 0x1AF5BE0 and the value is 4.
In method execution, the id address in myE is 0x1AF5BE0 and the value is 5.
After the method is executed, the id address in myE is 0x1AF5BE0 and the value is 5.
The execution process is as follows:
1) create an object myE In the heap and create a pointer to myE in the stack.
2) pass by reference, so create a pointer in the stack, copy the address of the pointer above, also point to myE
As shown in:
2. If the memory address inside the method points to a new object, the changes made in the method after the method ends will be discarded (the original pointer still points to the original object, method parameters point to the new object and are destroyed together ).
Unsafe class Program
{
Static void Main (string [] args)
{
Employee myE = new Employee () {id = 4 };
Fixed (int * pId = & (myE. id ))
{
Console. writeLine ("before method execution, the id address in myE is 0x {0: X} and the value is {1}", (uint) & (* pId), (uint) * pId );
}
TestMethod (myE );
Fixed (int * pId = & (myE. id ))
{
Console. writeLine ("after the method is executed, the id address in myE is 0x {0: X} and the value is {1}", (uint) & (* pId), (uint) * pId );
}
Console. ReadLine ();
}
Static void testMethod (Employee myE)
{
// MyE. id = 5;
MyE = new Employee () {id = 5 };
Fixed (int * pId = & (myE. id ))
{
Console. writeLine ("in method execution, the id address in myE is 0x {0: X}, and the value is {1}", (uint) & (* pId), (uint) * pId );
}
}
}
Output:
Before the method is executed, the id address in myE is 0x1E77BE0 and the value is 4.
In method execution, the id address in myE is 0x1E78E24 and the value is 5.
After the method is executed, the id address in myE is 0x1E77BE0 and the value is 4.
The execution process is as follows:
1) create an object myE In the heap and create a pointer to myE in the stack.
2) pass by reference, so create a pointer in the stack, copy the address of the pointer above, also point to myE
3) when a new object is added to the method, create an object in the heap and pass the address to pointer 2.
From heaven on the wall