【发布时间】:2014-02-27 11:04:49
【问题描述】:
我怀疑我只是误解了 Array 类的 Clone() 方法。 但是它显示为“创建 System.Array 的浅层副本”,所以我认为这意味着新的对象指针,而不是相同的对象指针......
以下情况真的应该发生吗?
假设一个测试对象...
public class testObject
{
public int propInt { get; set; }
}
还有下面的测试……
//create a the list
List<testObject> testList = new List<testObject>();
//add items to the list
testObject item1 = new testObject();
item1.propInt = 1;
testList.Add(item1);
testObject item2 = new testObject();
item2.propInt = 2;
testList.Add(item2);
//create what should be a COPY of the array
testObject[] testArray;
testArray = (testObject[])testList.ToArray().Clone();
foreach (testObject item in testArray)
{
item.propInt++;
}
//check items in list
foreach (testObject item in testList)
{
Trace.WriteLine("List:" + item.propInt);
}
//check items in coppied array
foreach (testObject item in testArray)
{
Trace.WriteLine("Array:" + item.propInt);
}
我希望结果是......
List:1
List:2
Array:2
Array:3
因为我假设当我进行克隆时,该克隆中的引用将指向具有与创建克隆时相同的属性的相同对象类型的新实例。此外,在修改它们之后,我会假设原始列表不会受到影响。
不喜,这个测试的输出其实是……
List:2
List:3
Array:2
Array:3
所以我在克隆上的操作似乎也反映在克隆的源中,这对我来说意味着它没有复制对象它复制了指向对象的指针,并且破坏了克隆方法开始的目的与..??
【问题讨论】:
-
这与 shallow 正好相反。被复制的只是数组本身
-
浅拷贝意味着它正在复制指针而不是创建新对象。
-
好的,我可以接受,MSDN 声明“数组的浅拷贝只复制数组的元素,无论它们是引用类型还是值类型,但它不会复制引用引用。新数组中的引用指向的对象与原始数组中的引用指向的对象相同。这似乎也支持这一点。那么我怎么才能得到不引用原件的副本呢?