这是我在Effective C#书中看到的例子:swaping集防御性副本
private BindingList<PayrollData> data;
public IBindingList MyCollection
{
get { return data; }
}
public void UpdateData()
{
// Unreliable operation might fail:
var temp = UnreliableOperation();
// This operation will only happen if
// UnreliableOperation does not throw an
// exception.
data = temp;
}
作者说这会为值类型而不是引用类型的工作。 我无法理解他的意思。
我想我现在明白了:一个集合是一个ref类型。 “数据字段”的消费者不会记得他们将副本保留在堆上的旧存储中。 如果“数据”是值类型 - 消费者(使用数据的其他代码)会记住他们持有数据的深层副本,并在需要更新时再次请求它。
对不对?
这里很难遵循你的思路。请详细说明。 –