如何列表中值数组保存在C#中不能引用 [英] how save array in list by value not reference in C#

查看:101
本文介绍了如何列表中值数组保存在C#中不能引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要救我的当前阵列状态,并在以后更改阵列,但是当保护我的名单排列,然后改变我的数组,数组中的名单也随之变化,这是样品code,这是我的$的一部分C $ C:

I want to save my current state of array and later change that array but when saver my array in list and then change my array , array in list also change , this is sample code and this is part of my code :

void main()
{
   List<int[]> lisarr = new List<int[]>();
   int[] a=new int[1];
   a[0]=1;
   lisarr.Add(a);
   a[0]=10;
   // at this time lisarr[0] also change
   ....
}

如果您跟踪此code通知,我的名单阵列还改变,但我不希望这样,我怎么能解决这个问题?

if you trace this code notice that my array in list also change but i don't want this,how can I fix this problem?

推荐答案

添加您的数组的一个副本:

Add a copy of your array:

List<int[]> lisarr = new List<int[]>();
int[] a=new int[1];
a[0]=1;
lisarr.Add(a.ToArray());
a[0]=10;

这篇关于如何列表中值数组保存在C#中不能引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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