WT86
WT86

Reputation: 833

How does Array.Reverse work?

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

Answers (2)

Bahadir Acar
Bahadir Acar

Reputation: 66

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

Upvotes: 2

Glorfindel
Glorfindel

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

Related Questions