I'm struggling to wrap my head around the issue I'm having when I'm running the program below:
using System;
namespace StringReversal
{
class MainClass
{
public static void Main(string[] args)
{
string example = "word";
char[] myArray = new char[example.Length];
myArray = example.ToCharArray(0, example.Length);
char[] reverseArray = new char[example.Length];
reverseArray = myArray;
Array.Reverse(reverseArray);
Console.WriteLine(myArray);
Console.WriteLine(reverseArray);
}
}
}
The output is as follows:
drow
drow
My best guess for the cause is that I'm using object references to assign myArray's value to reverseArrays, and then running the Array.Reverse() method on reverseArray.
While I'm sure this question has been discussed, I was wondering if I could get an explanation as to how this works in this manner. I assign values to variables all the time without an issue, like below:
int x = 10;
int y = 5;
x = y;
// x would then equal 5, correct?
Thanks for your insight!