如何在C#中的数组的深层副本? [英] How to make a deep copy of an array in C#?

查看:163
本文介绍了如何在C#中的数组的深层副本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我传递一个二维数组作为一个属性来我的用户控件。还有我在另一个二维数组存储此值:

I pass a two-dimensional array as a property to my user control. There I store this values in another two-dimensional array:

int[,] originalValues = this.Metrics;

后来我改变 this.Metrics 值。但现在,如果我检索值从originalValues​​,我从 this.Metrics 更改后的值。如何使一个深拷贝,不只是复制引用在C#?

Later I change values in this.Metrics. But now if I retrieve values from originalValues, I get the changed values from this.Metrics. How do I make a deep copy and don't just copy the references in C#?

推荐答案

您可以克隆一个数组,这使得它的一个副本:

You can clone an array, which makes a copy of it:

int[,] originalValues = (int[,])this.Metrics.Clone();

这篇关于如何在C#中的数组的深层副本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆