26

I have a little problem with arrays. I am new in C#.

I try to copy an int array into two other int arrays with

unsortedArray = randomNumbers();

unsortedArray2 = unsortedArray;
unsortedArray3 = unsortedArray;

But, if I sort the unsortedArray2, the unsortedArray3 is sorted too.

Can someone help me?

4
  • 3
    Array.Copy msdn.microsoft.com/en-us/library/… Commented Sep 6, 2017 at 8:42
  • You might want to read about "Pass by value" and "Pass by reference" to learn about why this is happening. Commented Sep 6, 2017 at 8:44
  • You have three references to the same array. So all your three variables pointing to the same array. Thous manipulating the array through one variable also makes the changes visible through the other ones. Commented Sep 6, 2017 at 8:44
  • I have been using thousands of arrays in my app and never ran into a problem until today. I wonder how that is possible. Commented Nov 22, 2021 at 2:26

8 Answers 8

33

I try to copy a Int Array into 2 other Int Arrays with

The first thing that is important is that in this line :

unsortedArray2 = unsortedArray;

you do not copy the values of the unsortedArray into unsortedArray2. The = is called the assignment operator

The assignment operator (=) stores the value of its right-hand operand in the storage location,

Now the second thing that you need to know to understand this phenomenon is that there are 2 types of objects in C# Reference Types and Value types

The documentation explains it actually quite nicely:

Variables of reference types store references to their data (objects), while variables of value types directly contain their data. With reference types, two variables can reference the same object; therefore, operations on one variable can affect the object referenced by the other variable.

The solution can be to use the Array.Copy method.

Array.Copy(unsortedArray, 0, unsortedArray2 , 0, unsortedArray.Length);

The CopyTo method would also work in this case

unsortedArray.CopyTo(unsortedArray2 , 0);

Note:this will work because the content of the array is a value type! If it would be also of reference type, changing a sub value of one of the items would lead also to a change in the same item in the destination array.

Sign up to request clarification or add additional context in comments.

Comments

4

The problem which you are struggling is not C# specific. The behavior will be the same almost in any programming language (JS/Java/Python/C). Variables unsortedArray2 and unsortedArray3 point to the same chunk of memory and when you reorder array you just manipulate with this memory piece.

Having this in mind, what do we need to achieve your goal? We need to copy array. It can be done in many ways

  1. Array.Copy
Array.Copy(unsortedArray, 0, unsortedArray2 , 0, unsortedArray.Length);
// The CopyTo method would also work in this case
unsortedArray.CopyTo(unsortedArray2 , 0);
  1. Creating new array and copy elements one by one in for loop
var unsortedArray2 = new int[unsortedArray.Length];
for (int i = 0; i < unsortedArray.Length; i++)
{
    unsortedArray2[i] = unsortedArray[i];
}
  1. Linq
var unsortedArray2 = unsortedArray.Clone();

Note: These operations are shallow copy which would work in your case because the contents are value types, not reference types.

1 Comment

normaly i program in SAP ABAP and there are no Arrays only tables :)
1

You can use Array.Copy:

unsortedArray = randomNumbers();

Array.Copy(unsortedArray, unsortedArray2 , unsortedArray.Length);
Array.Copy(unsortedArray, unsortedArray3 , unsortedArray.Length);

1 Comment

"You can use Array.Copy:" to do what? why will this solve the problem? why did the problem appear in the first place?
0

The array is a reference type, so when you sort the first, magically the second one is sorted. If you want to avoid that you have to copy your array (Use the copy method).

Comments

0

While this may not answer your titular question, you can use List:

List<int> unsortedArray2 = unsortedArray.ToList();

Doing so means you don't need to know the size of array you're copying, and you don't need to worry about it storing a reference and it's far easier to use than say:

int[] unsortedArray2 = new int[unsortedArray.Length];
unsortedArray.CopyTo(unsortedArray2, 0);

Comments

0

With the new collection expressions of C# 12 you can also just write

unsortedArray2 = [.. unsortedArray];

Comments

-1

This works for me, I think it's easier.

int[] unsortedArray = randomNumbers();

int[] unsortedArray2 = unsortedArray.ToArray();
int[] unsortedArray3 = unsortedArray.ToArray();

Comments

-3
unsortedArray = randomNumbers();

unsortedArray2 = unsortedArray.Clone;
unsortedArray.CopyTo(unsortedArray2, 0);

unsortedArray3 = unsortedArray.Clone;
unsortedArray.CopyTo(unsortedArray3, 0);

Array.Sort(unsortedArray2);

1 Comment

Your answer has no explanation whatsoever. How is dumping code supposed to help some one who is starting to learn the language?=!

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.