0

I dont understand why reversing the second array, also reverses the first array...Thanks to whoever can explain why.

char[] input = { 'h', 'e', 'l', 'l', 'o' };
char[] inputnew = input;     
Array.Reverse(inputnew);
Console.WriteLine(new string(input));
Console.WriteLine(new string(inputnew));

Output: olleh olleh

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263

8 Answers8

2

The first array (input) memory address is point to the second array (inputnew), so, any one place is changes is made effect the two variables.

because, two variables are pointing to the same memory address.

Reference types are always pointing to the memory address. the value types are always pointing to the value

more details http://net-informations.com/faq/general/valuetype-referencetype.htm

umasankar
  • 599
  • 1
  • 9
  • 28
2

With this line:

char[] inputnew = input;  

you copy the reference of array input to the reference of inputnew. This is how object oriented programing work. You always have reference of objects. That's why the types are also called Reference Type. On the other side you have Value types which actually copy the value, like int and boolean.

If you want to copy the actual values of your Array, you have to use the function Array.Copy(sourceArray, TargetArray) like this:

char[] input = { 'h', 'e', 'l', 'l', 'o' };
char[] inputnew;     

Array.Copy(input, inputNew);    

Array.Reverse(inputnew);
Console.WriteLine(new string(input));
Console.WriteLine(new string(inputnew));
user11909
  • 1,235
  • 11
  • 27
1

This is because the = doesn't create a copy of the array but rather you have two references pointing to the very same array.

In other words, there is a single array with two references pointing to it.

Wiktor Zychla
  • 47,367
  • 6
  • 74
  • 106
1

Well, the only possible answer — and may be the correct one — is that you're not actually copying the array. When you write

char[] inputnew = input; you're assigning a reference to the same array into another variable. They're both pointing at the same object, in other words.

Chetan Mehra
  • 189
  • 1
  • 3
  • 20
0

in c# arrays are passed by reference. inputnew and input has same indexing when allocation.

if you dont want to pass by reference :

char[] inputnew = input.copyTo();

will do the work.

Kutlu Ozel
  • 182
  • 1
  • 7
0

It is because you are not assigning a value when you do

char[] inputnew = input;

What you are doing is pointing inputnew to the location of { 'h', 'e', 'l', 'l', 'o' }; in the memory.

Therefore, you are getting the same output.

P.s. try to initiate the inputnew array first and then assign, see what happens.

Rarblack
  • 4,559
  • 4
  • 22
  • 33
0

To make a new array you should use the Array.Copy method, the way you have it now is that the inputNew is just a reference to input array.

your code should look something like this :

        char[] input = { 'h', 'e', 'l', 'l', 'o' };
        char[] inputnew = new char[input.Length];
        Array.Copy(input, inputnew, input.Length);
        Array.Reverse(inputnew);
        Console.WriteLine(new string(input));
        Console.WriteLine(new string(inputnew));
0

you're experiencing this problem because of the behavior of the types on memory. In fact, I suggest you look at the ValueType and reference type topics. Actually char [] inputnew = input; at this stage, you look at the input's inputnew (C ++ pointer), so if you want to do a process on the inputnew input process will refer to it as dependent lists. If you don't want to be affected by this process, you should look at the Clone cone.

look here

go..
  • 958
  • 7
  • 15