c# - 深度复制一个值类型的锯齿状数组而不进行序列化

标签 c# arrays serialization windows-phone-8 deep-copy

由于 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/

相关文章:

c# - Powershell - 全局注册 IO.FileSystemWatcher

javascript - 使用indexOf在对象数组中搜索特定属性

ios - 创建收集的图像数组

C# 有没有办法动态地从序列化中排除成员?

java - Spark Scala 中的任务不可序列化错误

c# - 扩展方法与静态方法优先级

c# - 什么在空花括号 block 中插入空格?

c# - 如何将可选指针参数从 C++ 代码转换为 C#

java - 如何在 Java 中使用多行 lambda 表达式对 int[] 进行排序

将字节数组转换为c中的结构体