我将类型double []的列表传递给类中的函数,使用tempList编辑函数中的值,然后返回编辑的值.但正在编辑传递的原始列表,我不希望它们被编辑以匹配tempList.
这是代码.
List<double[]> newList = new List<double[]();
newList = myClass.myFunction(value, originalList);
// myClass
...
// myFunction
public List<double[]> myFunction(int value, List<double[]> myList)
{
List<double[]> tempList = new List<double[]>();
for (int i = 0; i < myList).Count; i++)
{
tempList.Add(myList[i]);
}
// Do stuff to edit tempList
return tempList;
}
Run Code Online (Sandbox Code Playgroud)
请记住,数组是引用类型。当您将数组添加到 时tempList,仅添加对该数组的引用,因此myList和tempList都引用相同的double[]对象。
相反,您需要克隆数组:
for (int i = 0; i < myList.Count; i++)
{
tempList.Add((double[])myList[i].Clone());
}
Run Code Online (Sandbox Code Playgroud)