R
Richard Forester
Hello.
I need some help understanding what goes on when an array is copied. I
create 2 arrays and copy one to the other:
int[] pins = {9, 3, 7, 2};
int[] copy = new int[pins.Length];
for (int i = 0; i != copy.Length; i++)
{
copy = pins;
}
As I understand it, the two arrays are pointing to the same data in memory
because this is a shallow copy. However, if I change one of the elements of
the "pins" array and then echo the values of each array to the screen they
are not the same. For example:
pins[0] = 0;
for (int i = 0; i != pins.Length; i++)
{
Console.WriteLine(pins);
}
for (int i = 0; i != copy.Length; i++)
{
Console.WriteLine(copy);
}
Is the 0th reference of "pins" now pointing to another INT in memory? If
so, why would a deep copy ever be valuable since the reference is changed
whenever the array is altered? Or am I just getting this all wrong?
Thanks for your help,
Richard
I need some help understanding what goes on when an array is copied. I
create 2 arrays and copy one to the other:
int[] pins = {9, 3, 7, 2};
int[] copy = new int[pins.Length];
for (int i = 0; i != copy.Length; i++)
{
copy = pins;
}
As I understand it, the two arrays are pointing to the same data in memory
because this is a shallow copy. However, if I change one of the elements of
the "pins" array and then echo the values of each array to the screen they
are not the same. For example:
pins[0] = 0;
for (int i = 0; i != pins.Length; i++)
{
Console.WriteLine(pins);
}
for (int i = 0; i != copy.Length; i++)
{
Console.WriteLine(copy);
}
Is the 0th reference of "pins" now pointing to another INT in memory? If
so, why would a deep copy ever be valuable since the reference is changed
whenever the array is altered? Or am I just getting this all wrong?
Thanks for your help,
Richard