Problem with edit list <double []> in C #

I pass a list of type double [] to a function in the class, editing the values ​​inside the function using tempList, and then return the edited values. But the transferred source list is also being edited, and I do not want them to be edited according to 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; } 
+6
source share
8 answers

Keep in mind that arrays are reference types. When you add an array to tempList , only the array reference is added, so myList and tempList both refer to the same double[] objects.

Instead, you need to make a clone of arrays:

 for (int i = 0; i < myList.Count; i++) { tempList.Add((double[])myList[i].Clone()); } 
+4
source

The array, here double[] , is a reference type, so the string

 tempList.Add(myList[i]); 

Adds a reference to the source array. Then, when you edit tempList, you edit the original array. Make a copy as follows:

 tempList.Add(myList[i].ToArray()); 
+1
source

You add an array reference to the new list, but do not make a copy of the contents of each array. Your copy should look something like this:

 foreach (double[] item in myList) { double[] copy = new double[item.Length]; Array.Copy(item, copy); templist.Add(copy); } 
+1
source

The problem you are facing: double[] is the type of link, not the type of value, so when you add it to your tempList , you add a link to the original object, not to the new object. In fact, you need to create a new double[] before adding it to tempList so that you don't work on the original object.

Assuming you can use LINQ, you don't need to go in cycles. You can do something like:

 var tempList = myList.Select(x => x.ToArray()).ToList(); 
0
source

This is because Collections / reference types are passed by reference. (In fact, the holding variable is passed by value, but all variables point to the same link).

Detailed explanation read this SO answer

If you want the changes in my function not to be reflected in the original collection, you should copy / clone it and then go to myFunction .

Example

 newList = myClass.myFunction(value, (List<double>)originalList.Clone()); 
0
source
 tempList.Add(myList[i]); 

means that you add a reference to the double [] object on the index i in the list of topics. therefore, if you edit the value of this object, you will get a chance for both lists.

if you want to have different cloned lists that will not affect each other, you will need to do this:

 List<double[]> tempList = new List<double[]>(); for (int i = 0; i < myList).Count; i++) { double[] originalListItem = myList[i]; // the most important step here!!! - clone the originalListItem to clonedListItem tempList.Add(clonedListItem); } // Do stuff to edit tempList return tempList; 
0
source

You copy the double [] link to the new list, this is a shallow copy. You need a deep copy and the creation of new double arrays to edit temp arrays without changing the original arrays.

0
source

You are inserting array references in tempList, not a copy of the array. Therefore, if you change the value in tempList, you change the original array.

This code will work better:

  for (int i = 0; i < myList.Count; i++) { var copy = new Double[myList[i].Length]; Array.Copy(myList[i], copy, myList[i].Length); tempList.Add(copy); } 
0
source

Source: https://habr.com/ru/post/916573/


All Articles