I am passing a list of type double[] to a function in a class, editing the values within a function using a tempList, then returning the edited values. But the originalList being passed is being edited as well which I do not want them edited to match the tempList.
Here is the code.
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;
}
Bear in mind that arrays are reference types. When you add an array to tempList
, only a reference to the array is being added, so that myList
and tempList
both refer to the same double[]
objects.
Instead, you need to make a clone of the arrays:
for (int i = 0; i < myList.Count; i++)
{
tempList.Add((double[])myList[i].Clone());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With