0

I have this simple piece of code..

bool isTrue(char[] number)
{
    char[] reverse = number;
    Array.Reverse(reverse);
}

When debugging the application I saw that number is reversed too. Can someone explain to me why? Is it related to how char arrays work, or am I missing something?

Sender
  • 6,660
  • 12
  • 47
  • 66
WT86
  • 823
  • 5
  • 13
  • 34

2 Answers2

5

With

char[] reverse = number;

you are not creating a copy of the array, but just another reference to it.

If you want to copy the array, you can use .Clone():

char[] reverse = number.Clone();
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
2

Try using number.CopyTo(reverse, 0) instead of char[] reverse = number;