You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#引用类型按值传递代码疑问:为何赋值操作不影响输出?

Why does the output stay 10, 50, 100? Let's unpack this C# reference behavior

Great question—this trips up a lot of folks learning C#! The key here is understanding how reference types are passed as parameters in C# (it's "pass-by-value of the reference," not "pass-by-reference" by default). Let's break this down step by step, with a mental "diagram" to visualize what's going on.

First, let's recap your code clearly

Here's your code formatted for readability:

static void Main(string[] args) 
{ 
    List<int> myList = new List<int>(); 
    myList.Add(100); 
    myList.Add(50); 
    myList.Add(10); 
    ChangeList(myList); 
    foreach (var item in myList) 
    { 
        Console.WriteLine(item); 
    } 
    Console.ReadLine(); 
}

private static void ChangeList(List<int> myList) 
{ 
    myList.Sort(); 
    List<int> myList2 = new List<int>(); 
    myList2.Add(3); 
    myList2.Add(4); 
    myList = myList2; // This line doesn't affect Main's myList!
}

Step 1: What happens before calling ChangeList?

In Main, you create a List<int> (let's call this List A) and add 100, 50, 10. The variable myList in Main is a reference—it's like a pointer that points to List A on the heap:

Main's myList → [List A: 100, 50, 10] (stored in the heap)

Step 2: Calling ChangeList(myList)

When you call ChangeList(myList), you're passing a copy of that reference to the method. So inside ChangeList, there's a separate variable also named myList, but it's just a copy of the reference from Main. Both variables point to the same List A:

Main's myList → [List A: 100, 50, 10]
ChangeList's myList → [List A: 100, 50, 10] (copy of Main's reference)

Step 3: myList.Sort() does the real work

When you call myList.Sort() inside ChangeList, you're modifying the actual List A object on the heap (since both references point to it). Now List A is sorted:

Main's myList → [List A: 10, 50, 100] (sorted in-place)
ChangeList's myList → [List A: 10, 50, 100] (still pointing to the same sorted list)

Step 4: Creating myList2 and assigning myList = myList2

Next, you create a new List<int> (List B) with 3 and 4. Then you do myList = myList2—but this only affects the local myList variable inside ChangeList. It changes that variable's reference to point to List B instead of List A. But the myList variable in Main is completely unaffected; it still points to List A:

Main's myList → [List A: 10, 50, 100] (unchanged!)
ChangeList's myList → [List B: 3, 4] (now points to the new list, but only inside this method)
ChangeList's myList2 → [List B: 3, 4]

Why does commenting out myList = myList2 not change the output?

Because that line never touches List A—the list that Main is going to iterate over. The only modification to List A is the Sort() call, which already happened before that line. Whether you reassign the local reference or not, List A remains sorted, so Main will always print 10, 50, 100.

If you wanted to change Main's myList to point to List B...

You'd need to use the ref keyword to pass the reference by reference (instead of passing a copy of the reference). Modify the method signature and call like this:

private static void ChangeList(ref List<int> myList) 
{ 
    // ... same code ...
    myList = myList2; // Now this affects Main's myList!
}

// In Main:
ChangeList(ref myList);

Now, when you run the code, the output would be 3, 4 instead—because you're modifying the original reference from Main to point to List B.

内容的提问来源于stack exchange,提问作者Vivek Shukla

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:40:53