List passed by ref - help me explain this behaviour
Take a look at the following program:
class Test
{
List<int> myList = new List<int>();
public void TestMethod()
{
myList.Add(100);
myList.Add(50);
myList.Add(10);
ChangeList(myList);
foreach (int i in myList)
{
Console.WriteLine(i);
}
}
private void ChangeList(List<int> myList)
{
myList.Sort();
List<int> myList2 = new List<int>();
myList2.Add(3);
myList2.Add(4);
myList = myList2;
}
}
I assumed myList
would have passed by ref
, and the output would
3
4
The list is indeed "passed by ref", but only the sort
function takes effect. The following statement myList = myList2;
has no effect.
So the output is in fact:
10
50
100
Can you help me explain this behavior? If indeed myList
is not passed-by-ref (as it appears from myList = myList2
not taking effect), how does myList.Sort()
take effect?
I was assuming even that statement to not take effect and the output to be:
100
50
10
Solution 1:
Initially, it can be represented graphically as follow:
Then, the sort is applied myList.Sort();
Finally, when you did: myList' = myList2
, you lost the one of the reference but not the original and the collection stayed sorted.
If you use by reference (ref
) then myList'
and myList
will become the same (only one reference).
Note: I use myList'
to represent the parameter that you use in ChangeList
(because you gave the same name as the original)
Solution 2:
You are passing a reference to the list, but your aren't passing the list variable by reference - so when you call ChangeList
the value of the variable (i.e. the reference - think "pointer") is copied - and changes to the value of the parameter inside ChangeList
aren't seen by TestMethod
.
try:
private void ChangeList(ref List<int> myList) {...}
...
ChangeList(ref myList);
This then passes a reference to the local-variable myRef
(as declared in TestMethod
); now, if you reassign the parameter inside ChangeList
you are also reassigning the variable inside TestMethod
.