Reputation: 833
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?
Upvotes: 0
Views: 256
Reputation: 66
Try using number.CopyTo(reverse, 0) instead of char[] reverse = number;
Upvotes: 2
Reputation: 22651
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();
Upvotes: 5