C# Value and Reference types

前端 未结 6 1278
灰色年华
灰色年华 2021-01-06 15:55

Please see the following lines of code mentioned below:

byte[] a = { 1, 2, 3, 4 };
byte[] b = a; // b will have all values of a.
a = null; 

6条回答
  •  醉酒成梦
    2021-01-06 16:14

    First you create an array somewhere in the memory, for example starting at address 1000. a is a reference, it does not contain the array, it contains the address 1000. b also contains this address. In line 3 you change a to point to null instead, but b is still pointing at the array in address 1000.

    You edited the reference (a), you did not edit the object it referenced to ({1,2,3,4}).

提交回复
热议问题