0

The following code successfully swaps the values in array b and array c. Since array is a reference type, I dont expect the code should work. The execution of "testb = testc" should change the value pointed to by testa. So, by the end of execution, I expect testb = testc = {4,5}. Can someone explain why that is not so? TIA!

    static void Main(string[] args)
    {
        int[] testa = null;
        int[] testb = new int[] { 3, 4 };
        int[] testc = new int[] { 4, 5 };
        testa = testb;
        testb = testc;
        testc = testa;
    }
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • Yes, `int[]` is a reference type, but the variables are values that point to those reference type instances. – ProgrammingLlama Dec 21 '21 at 06:02
  • It looks like you think reference type assignment works differently than how it really behaves. When you have a reference type, assigning a new value to the reference type does NOT change the object it originally pointed to. Rather, it makes it point to a new object. – Gonen I Dec 21 '21 at 06:15

2 Answers2

4

The execution of "testb = testc" should change the value pointed to by testa

I think the fundamental thing you're missing is that references do not chain

    int[] testa = null;
    int[] testb = new int[] { 3, 4 };
    int[] testc = new int[] { 4, 5 };

We now have in ascii art:

testa -->
testb --> [3,4]
testc --> [4,5]

Next

    testa = testb;

We now have:

testb --> [3,4] <-- testa
testc --> [4,5]

Then

    testb = testc;

Gives us

          [3,4] <-- testa
testc --> [4,5] <-- testb

Finally

    testc = testa;

Results in

testc --> [3,4] <-- testa
          [4,5] <-- testb

There was never a chain of

 testa --> testb --> [3,4]

or anything like that such that changing testb also changed testa

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • I Can't be sure what the OP was thinking, but my guess is that he didn't think about reference chaining. I believe he held the ( very common ) misconception that assigning to a reference changes the value of the underlying original object that the reference pointed to. – Gonen I Dec 21 '21 at 08:37
  • I'd be delighted to extend the answer if the OP clarifies or points out that they didn't understand or I missed their point – Caius Jard Dec 21 '21 at 08:55
2

Array is reference type, and assignment for reference type is telling them to point to the same instance. For example, testa = testb means "make testa points to the instance that testb currently pointing to". So your code is basically like:

    int[] testa = null;
    int[] testb = new int[] { 3, 4 };
    int[] testc = new int[] { 4, 5 };
    testa = testb; // point testa to { 3, 4 }
    testb = testc; // point testb to { 4, 5 }
    testc = testa; // point testc to where testa is pointing, 
                   // which is { 3, 4 } because the first assignment

However, the code can also be used for swapping value types. It is a basic pattern for swapping two values.

tia
  • 9,518
  • 1
  • 30
  • 44