如何制作数组的浅表副本? [英] How to make a shallow copy of an array?
本文介绍了如何制作数组的浅表副本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将二维数组作为属性传递给我的用户控件。在那里,我将此值存储在另一个二维数组中:
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
获取更改后的值。如何复制 this.Metrics
的元素,而不仅仅是获取数组的引用?
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 copy of the elements of this.Metrics
and don't just get the reference of the array?
推荐答案
您可以克隆一个数组,并复制该数组:
You can clone an array, which makes a copy of it:
int[,] originalValues = (int[,])this.Metrics.Clone();
这篇关于如何制作数组的浅表副本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文