Skip to main content
 首页 » 编程设计

c#之清空对对象的所有引用

2024年02月20日12bluestorm

<分区>

我有一个 Simple 类,它包含一个整数 number。 使用此代码:

Simple b = new Simple(); 
List<Simple> list = new List<Simple>(); 
list.Add(b); 
b.number = 6; 
b = null; 
Debug.WriteLine("The value of b is " + b); 
Debug.WriteLine("And the value of the clown in the list is " + list[0].number); 

Debugs 返回b 的值为 和列表中 clown 的值为6。

我可以假设通过将 b 添加到 list,然后存储对 b 的引用。然后,通过清空 b,list 仍然包含对 b 对象的引用,因此可以打印出 number 的值。

但是,如果我可以更改 b 的成员并通过存储在列表中的引用反射(reflect)它,那么为什么 b = null 不反射(reflect)list[0] 的值?我只能假设这与值(value)和引用有关。

帮助任何人?