I don't understand why the reference point for both list2 and list2 has changed with the code below.
Note: I am not familiar with C#.
private void TestMethod(ref List<String>list2){
list2 = new List<String>();
list2.Add("bye-bye!");
}
List<String>list=new List<String>();;
list.Add("Hello!");
TestMethod (reflist);
System.Console.WriteLine(list[0]); // "bye-bye!"
list2=new List<String>();
You can see that a new address has been reserved in this part.However, it seems that the reference point of the list has changed, but what kind of mechanism is it?Do you store information about the existence and location of two variables, list and list2, in a common reference point between list and list2?
Also, please let me know how PHP and javascript behave (I know that Java does not have a strict reference pass, but "copy and pass the reference address").
python javascript java c#
I think it would be easier to understand if you look at the conceptual diagram of the situation in memory roughly.
After the following two lines of execution, the stack heap at runtime (conceptually) appears as follows:
List<String>list=new List<String>();;
list.Add("Hello!");
stack
Browse to list-> | List<String> |
|
|
+-----+
|
| heap
+- > | List contents referenced by `list' |
|[0] "Hello!" |
(Actually, strings can also be referenced, so the references are more complicated, but the non-essential parts are simplified.)
If you invoke TestMethod(ref list);
, the real argument list2
in TestMethod
looks like this:
stack
list+-> | Browse to List<String> |
| |
+--+ |
| |
| +-----+
| |
| |heap
| +- > | List contents referenced by `list' |
| |[0] "Hello!" |
|
+----+
|
list2-+
The list
and list2
represent the same variable.Internally, it passes the address of the variable.In other words, "reference passing" means passing a reference to a variable.
In TestMethod
in this state,
list2 = new List<String>();
list2.Add("bye-bye!");
This is what it looks like when is run.
stack
list+-> | Browse to New List<String> |
| |
+--+ |
| |
| +-----+
| |
| |heap
| | | Contents of the list referenced in the original `list' |
| |[0] "Hello!" |
| |(↑It's not referenced anywhere, so it'll be released soon)
| |
| +- > | Contents of List referenced by `list2`(`list`)|
| |[0] "bye-bye!" |
|
+----+
|
list2-+
On the other hand, if you use C# to pass the price, and if you only have to pass the price like Java, immediately after the call, it looks like this:
stack
Browse to list-> | List<String> |
|
|
+-----+
|
| heap
+--+-> | List contents referenced by `list' |
| |[0] "Hello!" |
|
+--------+
| stack
list2->|See List<String>|
Both list
and list2
refer to the same entity in the heap, but they are assigned to a different location as variables.
To sum up roughly,
I think it means that
I am not good at drawing and explaining, but I hope that you will be able to understand it through matching techniques.If you have any questions, please let me know.
If you know C language, C++, you may feel refreshed if you think of C# as a pointer.
© 2024 OneMinuteCode. All rights reserved.