redfiloux
redfiloux

Reputation: 113

C# : Changing copy of array changes array itself

I have a very basic question in C#. So I have an array of int called m_permutation (property of a class), and in a method of the class I have the following code:

    int[] newPermutation = new int[m_permutation.Length];
    newPermutation = m_permutation;
    newPermutation[0] = 5;

I am confused as to why m_permutation is also changed in this code, and how can I fix it ?

I understand that I can initialize newPermutation via a loop for, to get the same values of m_permutation, and that fixes it. However, can somebody explain why this happens, and what is the best fix ?

Thank you,

Bogdan

Upvotes: 0

Views: 1531

Answers (2)

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391724

There's some good, and some bad, about your expectations for your code.

Let's go through what your code is actually doing:

int[] newPermutation = new int[m_permutation.Length];

This will declare a new variable, newPermutation, to be an array of ints, and then construct a new int array containing m_permutation.Length elements.

So far so good.

The next line, not so much:

newPermutation = m_permutation;

This line will actually replace the reference in your array variable newPermutation to, after the assignment, refer to the same array as m_permutation.

Let's consider what an array variable actually is.

When you do this:

int[] x = new int[5];

Then you're doing a couple of things:

  1. You're declaring a variable, x
  2. You're constructing a new object containing the int array
  3. You're assigning the variable, x to refer to this object

After the 2nd line:

newPermutation = m_permutation;

you're essentially saying this:

  1. OK, you know that array we just constructed? Forget that
  2. Let's now refer to this other array, the one that the variable m_permutation is also referring to.

So when this line executes:

newPermutation[0] = 5;

You're essentially saying: The array that newPermutation is now referring to, its first element should now have the value 5.

Since newPermutation at this point refers to the same array as m_permutation, it appears that you're modifying an additional array but in reality you only have one array. You do, however, have two variables referring to the same array.

I recommend you read my answer here regarding pointers since this is relevant.

However, there is an easy fix to your problem.

You can ask for a copy of the array, instead of a reference to the original one.

Simply change your code to this:

int[] newPermutation = m_permutation.ToArray();

The .ToArray() method is guaranteed to return a new array, so this won't be shared with the original.

Bear in mind, however, that if you do this with anything more complex than an int, such as an object, you're only getting copies of the object references, not the objects themselves. You can get back to Stack Overflow with new questions when/if you get to this point.

Upvotes: 8

Because they both reference the same object in memory. You can use,

Array.Copy(m_permutation, newPermutation,m_permutation.Length );

Upvotes: 2

Related Questions