由于 Windows Phone 没有 System.Runtime.Serialization.Formatters.Binary 命名空间,我使用以下方式:
bool[][] newMask = (bool[][])this.mask.Clone();
但我不确定这是否会进行深拷贝(尽管 this 问题表明我会进行深拷贝,但我怀疑我使用锯齿状数组来提高性能)
最佳答案
这只会生成一个浅拷贝。要制作深拷贝,您需要类似以下内容:
bool[][] newMask = new bool[mask.Length][];
for (int i = 0; i < newMask.Length; i++)
{
newMask[i] = (bool[]) mask[i].Clone();
}
来自 Array.Clone
的文档:
Creates a shallow copy of the Array.
...
A shallow copy of an Array copies only the elements of the Array, whether they are reference types or value types, but it does not copy the objects that the references refer to. The references in the new Array point to the same objects that the references in the original Array point to.
关于c# - 深度复制一个值类型的锯齿状数组而不进行序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19252600/