Today, when I browsed the blog post, I saw this article: in C #, what is the pass in and out of ref? Questions about using ref when uploading objects
Additional Remarks:
This article was written in the morning and was just about to be released. The company was disconnected from the network and the original modification was lost.
Once the network is disconnected, it will be released after one day.
We always know that the reference type is the address, and the value type is the value, but there are still so many people confused. Although many articles about ref are circulating on the internet, it seems that I have not been confused for everyone.
Recently, when someone was reading my CYQ. Data framework, since the class is a reference address, it is actually an address. What else do you need to write a ref file in your code;
As I understood before, I was fooled into failing to understand it, and I thought I was wrong with the original code.
Today, when I was helping people solve their problems, I had to solve them.
Here I have modified the code for the previous article's original problem.
The following is an example:
/// <Summary>
// By passing by autumn http://cyq1162.cnblogs.com
/// </Summary>
Class Program
{
Static void Main (string [] args)
{
Person p = new Person ("James ");
Change (p );
Write (p. Name); // The output is that John is not Li Si.
Change (p, "Li Si ");
Write (p. Name); // The output is Li Si.
Change (ref p );
Write (p. Name); // The output is Wang Wu.
Console. ReadKey ();
}
Static void Change (Person p)
{
P = new Person ("Li Si ");
}
Static void Change (Person p, string name)
{
P. Name = name;
}
Static void Change (ref Person p)
{
P = new Person ("Wang Wu ");
}
}
By the way, let's see how debugging can solve the problem.
F5 run:
1: we run the first line of code, breakpoint:
Important Notes:
P address: 0x044becf8
2: we run it in Change (p ).
Note: The p address is changed: 0x044becd0. To put it simply, your internal p address is different from the external p address, so it does not affect the external p address.
The address of the P value in the remarks changes because the breakpoint has not been instantiated in the first line of code, so its value is the same.
So the output of the first line is: James
3: Next, run the Change (p, "Li Si") function.
Note: The internal p address is changed to 0x044becd0.
However, it points to the same value, so you changed the value, so "Li Si" came out.
Note:
Although the internal p is different from the external p, it points to the same value..
So the output of the first line is: Li Si
4: finally, it is inside the Change (ref p) function.
Important Notes:
The internal p address is the same as the external p address..
Then point to a new object, but the value address of the object has changed.
Note: The External p address = the internal p address, so it points to the value of the new object.
The output of the third line is: Zhang San
The final result is shown as follows:
Conclusion:
Use ref for the Class type to ensure that the referenced addresses are consistent.
Therefore, do not just realize the reference type transfer address. It is enough to pass the value of the value type. It should be noted that when the reference type is transferred, internal parameters will generate a new address.
Therefore, when passing class references, we should pay attention to the internal usage of new. This seems to be a problem in the field of parameter passing, but the ref is attached.