32

I used this code to copy one 2D array to another 2D array:

Array.Copy(teamPerformance, 0,tempPerformance,0, teamPerformance.Length);

However, when I change some data in tempPerformance then these changes also apply to teamPerformance.
What should I do to control that?

5 Answers 5

76

You need Clone()

double[,] arr = 
{
   {1, 2},
   {3, 4}
};
double[,] copy = arr.Clone() as double[,];
copy[0, 0] = 2;
//it really copies the values, not a shallow copy, 
//after:
//arr[0,0] will be 1
//copy[0,0] will be 2
Sign up to request clarification or add additional context in comments.

3 Comments

I prefer this answer. Also, I would suggest the assignment to look like this: double[,] copy = (double[,])arr.Clone();
Clone() creates a shallow copy by copying the value of each array element onto the new array. For value types, this effectively creates a deep copy, but it will not work for an array of objects or an array of arrays, like in dasblinkenlight's example. Demo
While this works, Array.Copy() also does the exact same thing when you have a multidimensional array of value types like double as in the above example. This is rather useful when you have a jagged array (double[][] instead of double[,]) and when the inner arrays have to be cloned. The OP is most likely referring to a jagged array or a multidimensional array of reference types.
23

This is correct: Array.Copy performs a shallow copy, so the instances of arrays inside the inner dimension get copied by reference. You can use LINQ to make a copy, like this:

var copy2d = orig2d.Select(a => a.ToArray()).ToArray();

Here is a demo on ideone.

1 Comment

Surely this is only for jagged arrays, as Select() is not defined for 2D arrays.
3

According to MS(http://msdn.microsoft.com/en-us/library/z50k9bft.aspx):

If sourceArray and destinationArray are both reference-type arrays or are both arrays of type Object, a shallow copy is performed. A shallow copy of an Array is a new Array containing references to the same elements as the original Array. The elements themselves or anything referenced by the elements are not copied. In contrast, a deep copy of an Array copies the elements and everything directly or indirectly referenced by the elements.

Comments

3

From what I understand reading the question, I assume the arrays are filled with reference-types, but C# does not provide a way to "just copy" a reference-type. It will only copy the reference to the object (so, then 2 things will point to the same thing).

So, putting reference-types in an array and then making a copy of the array, will not "just" make a copy of a reference-type (because C# can't do that, within an array or not).

That's why 0 of the answers given here will solve the problem. Sheng's answer is correct (but will not solve the problem) -and could maybe use a bit more explaining.

If you want to make copies of references-types, you should look into ways of doing exactly that. Like, converting them to and from JSON, or make a copy-method yourself, maybe by using Reflection.

1 Comment

Precisely. You can't perform a deep copy unless you tell c# how to perform a deep copy.
0

You can also use:

int[] copy = new int[old.Length];
Buffer.BlockCopy(old, 0, copy, 0, old.Length * 4];

Note that you have to know the number of bytes you want to copy. Besides that, this method should be about best in terms of performance.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.