C# Value and Reference types

前端 未结 6 1231
灰色年华
灰色年华 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}).

    0 讨论(0)
  • You are making b point to the memory address that contains the byte array. Then afterwards, you are making a point to null. b remains unchanged.

    0 讨论(0)
  • 2021-01-06 16:20

    When a type is a value type, it means that the variables of this type store the actual data. When a type is a reference type, it means that the variables of this type store a reference to the actual data.

    In both cases, assignment from one variable to another copies the content of the variable. For value types, this means that the value is copied from one variable to the other, duplicating the actual data and thus creating a new object. For reference types, this means that the reference is copied from one variable to the other, duplicating the reference but leaving the actual data intact. To stress that: the object isn't copied, but the reference is. The two copies of the reference are independent, even though it's easy to observe that they point to the same object when dereferenced.

    From these you can easily see that the assignment from one variable to the other copies the reference to the byte array, so you have two references to the byte array. Afterwards, when you assign null to one of the variables, you are copying a null reference to it, overwriting the original reference (which was stored in that variable) to the byte array. So, now you don't have two references to the byte array, but only one again - the object is the same, but the references to it are independent.

    (Similarly, if you reassign the byte array back to the nulled variable, it doesn't mean that all variables in your program that pointed to null will point to the byte array - only the one you assigned will.)

    0 讨论(0)
  • 2021-01-06 16:24

    A reference type variable points to a value. When you assign one ref type variable to another you copy reference to its value not to the variable itself. This is why b still points to a table of values.

    0 讨论(0)
  • 2021-01-06 16:32

    That's actually how reference types works.

    As you said, byte[] is a reference type like all other arrays. Let's analyze your sample line by line;

    byte[] a = { 1, 2, 3, 4 };
    

    --> You created a byte array in memory and a is a reference that array.

    enter image description here

    byte[] b = a;
    

    --> Your b is referencing the same object with a which is { 1, 2, 3, 4 } but they are different references.

    enter image description here

    a = null;
    

    --> Your a is not referencing anywhere in the memory but that doesn't effect b.

    enter image description here

    0 讨论(0)
  • 2021-01-06 16:33

    Your question makes the assumption that when you make the assignment:

    byte[] b = a;
    

    And that you're making some sort of graph association like so:

    b -> a -> { 1, 2, 3, 4 }
    

    And when you make the assignment of a to null, you impact the value of b, because:

    b -> a -> null
    

    But that's not how copying references work. When you copy the reference, you really make a copy of the reference that a has, like so:

    a ----> { 1, 2, 3, 4 }
                  ^
    b ------------|
    

    This is why when you make the assignment of a to null, you don't impact the value of b, just a:

    a ----> null      { 1, 2, 3, 4 }
                              ^
    b ------------------------|
    

    0 讨论(0)
提交回复
热议问题