如何通过值而不是引用将数组添加到列表? [英] How to add an array to a list by value not by reference?

查看:54
本文介绍了如何通过值而不是引用将数组添加到列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过值而不是通过引用将数组添加到数组列表中?

Is there a way to add an array to a list of arrays by value and not by reference?

示例:以下打印出"6、7、8、9、10".我希望它写出"1、2、3、4、5".

Example: The following prints out "6, 7, 8, 9, 10". I want it to write out "1, 2, 3, 4, 5".

int[] testArray = new int[5] { 1, 2, 3, 4, 5 };
List<int[]> testList = new List<int[]>();

testList.Add(testArray);

testArray[0] = 6;
testArray[1] = 7;
testArray[2] = 8;
testArray[3] = 9;
testArray[4] = 10;

foreach(int[] array in testList)
{
    Console.WriteLine("{0}, {1}, {2}, {3}, {4}", array[0], array[1], array[2], array[3], array[4]);
}

推荐答案

进行复制:

testList.Add(testArray.ToArray());

这篇关于如何通过值而不是引用将数组添加到列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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