Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# “C”;“有趣”;列表问题<;字符串[]>;_C#_Arrays_List_Inheritance - Fatal编程技术网

C# “C”;“有趣”;列表问题<;字符串[]>;

C# “C”;“有趣”;列表问题<;字符串[]>;,c#,arrays,list,inheritance,C#,Arrays,List,Inheritance,我的C#应用程序中的列表有一些奇怪的问题。这一定是分配错误,或者是我做错了什么(我是一个普通的C#开发者)。 让我举一个与我的思路相近的例子: List<String[]> MyPrimaryList = new List<String[]>(); List<String[]> MySecondaryList = new List<String[]>(); String[] array; String arrayList = "one,two,t

我的C#应用程序中的列表有一些奇怪的问题。这一定是分配错误,或者是我做错了什么(我是一个普通的C#开发者)。 让我举一个与我的思路相近的例子:

List<String[]> MyPrimaryList = new List<String[]>();
List<String[]> MySecondaryList = new List<String[]>();
String[] array;

String arrayList = "one,two,three,four,five";
array = arrayList.Split(',');

MyPrimaryList.Add(array);
MySecondaryList.Add(array);

MyPrimaryList[0][0] += "half";
List MyPrimaryList=new List();
List MySecondaryList=新列表();
字符串[]数组;
String arrayList=“一、二、三、四、五”;
array=arrayList.Split(',');
添加(数组);
添加(数组);
MyPrimaryList[0][0]+=“一半”;
所以现在我希望MyPrimaryList中第一个数组的第一个值是MySecondaryList中的“一半”和“一”。 但我的问题是,两个列表都会更新为“onehalf”,作为两个列表中第一个数组的第一个值

你有好的解释吗?:)


谢谢

您正在向两个列表添加相同的数组实例,因此它们指向相同的内存结构


如果你想让它们独立,那么你需要克隆它们;一个快速的方法是在其中一个列表中使用linq
list.Add(array.ToArray())
,数组是引用对象,因此您正在修改内存中的相同集合。你所做的就是向同一个结构添加指针

请查看此文档

您需要对阵列进行深度复制,以获得所需的自主权


您可以使用

String[]数组执行此操作是一种引用类型。您已将此对象在内存中的位置的引用添加到两个列表中,因此它们都包含相同的数据

如果需要第二个列表来获得
数组的副本,则可以使用:


由于每个列表现在都包含对不同数组的引用,因此您可以独立地操作数组项。

您应该为正在使用的每个列表创建一个数组,因为现在您指向的是相同的内存结构

正确的代码应为:

List<String[]> MyPrimaryList = new List<String[]>();
List<String[]> MySecondaryList = new List<String[]>();
String[] array;
String[] secondaryArray;

String arrayList = "one,two,three,four,five";
array = arrayList.Split(',');
secondaryArray = arrayList.Split(',');

MyPrimaryList.Add(array);
MySecondaryList.Add(secondaryArray);

MyPrimaryList[0][0] += "half";
List MyPrimaryList=new List();
List MySecondaryList=新列表();
字符串[]数组;
字符串[]二次数组;
String arrayList=“一、二、三、四、五”;
array=arrayList.Split(',');
secondaryArray=arrayList.Split(',');
添加(数组);
MySecondaryList.Add(secondaryArray);
MyPrimaryList[0][0]+=“一半”;

现在,您的第二个列表将具有“one”而不是“onehalf”,因为前面提到的数组是引用类型,因此这两个列表将指向同一个数组


要解决您的问题,请使用方法


虽然它会对该数组进行浅层复制,但它适用于您的情况

您将同一数组添加到两个列表中,因此它们都指向您正在更改的数组元素。您将同一对象添加到两个列表中-换句话说,只有一个
数组
您不在任何位置创建副本…
。选择(x=>x)不需要
,因为
string[]
已经实现了
IEnumerable
@Eren,你是对的,我这样做是因为我不能100%确定对数组调用的ToArray会返回一个新副本。但我检查过了,这就是它的工作方式
onehalf
one
List<String[]> MyPrimaryList = new List<String[]>();
List<String[]> MySecondaryList = new List<String[]>();
String[] array;
String[] secondaryArray;

String arrayList = "one,two,three,four,five";
array = arrayList.Split(',');
secondaryArray = arrayList.Split(',');

MyPrimaryList.Add(array);
MySecondaryList.Add(secondaryArray);

MyPrimaryList[0][0] += "half";
MyPrimaryList.Add((String[])array.Clone());
MySecondaryList.Add((String[])array.Clone());