Regarding object = new when another reference type points to it

96 views Asked by At

I am vary curious regarding a specific case in c#: In this code, pointToSourceArr points to source arr, but if I change sourceArr by a setting it to a new array or setting it to null, pointToSourceArr remains with it's two objects : { "Itamar", "sasha" }. Why is that?

string[] sourceArr = new string[] { "Itamar", "sasha" };
string[] pointToSourceArr = sourceArr;
sourceArr = new string[]() // OR NULL;
3

There are 3 answers

1
Jon Skeet On

Why is that?

The value of each variable is just a reference to the array. The assignment here:

string[] pointToSourceArr = sourceArr;

just copies the existing value of sourceArr into pointToSourceArr. It doesn't set up a long-lasting relationship between the two variables.

Imagine I had a piece of paper with my home address written on it... and then I photocopy that piece of paper and give it to you. If I then cross out the address on my piece of paper, that doesn't change your piece of paper, does it? It's exactly the same here.

Note that if instead you'd change some data within the array, e.g.

sourceArr[0] = "Foo";

then that would be equivalent to me going to my house and painting the front door red - in which case if you then went to the address shown on your piece of paper, you would see a red front door. It's the difference between changing the value of a variable (no other variables are changed) and changing the value within an object (which means anything which can see that object can observe the change).

4
Ondrej Janacek On

Because pointToSourceArr does not point to the variable sourceArr which is also a "pointer" (reference, in fact) but directly to this object new string[] { "Itamar", "sasha" }; you created. And when you change where sourceArr variable points to, it does not affect where pointToSourceArr points to.

The following picture shows states before and after assigning null to the sourceArr variable.

enter image description here

1
Needham On

This is because when your code executes it executes in order. So when you initialize sourceArr is has two values, {"Itamar",sasha}. Since your code executes in order, when you initialize pointToSourceArr and set it to e the same as sourceArr it becomes basically another version of sourceArrbut still has the same values as sourceArr at this point. Now, once you change the values of souceArr since your code has already executed the first two lines it the pointToSourceArr is still going to be the same as it originally was all that is changed is the value of the sourceArr.

to explain this point more clearly, my name is Luke, imagine that I was named after my father, who was also called Luke. in code terms if would be fair to say myName = fathersName, now imagine that my father changed his name, my name would still be Luke, however his name would be something else.

Finally, if you want pointToSourceArr to be the same as SouceArr again, you need to once again write pointToSourceArr = sourceArr; after the value of sourceArr has changed.

Hope to have helped.