Search code examples
c#.netnullpass-by-referenceassignment-operator

After assignment referance object is not null, how is possible?


The object a is null, but I saw the object b was not null in runtime. Why object b is not null in the case?

List<int> a = new List<int>();
List<int> b = a;
a = null;

Solution

  • With this statement a and b 'point' to the same object:

    List<int> b = a;
    

    And when you do

    a = null;
    

    a 'points' to something different while b is untouched (and still points to the list).

    Neither a nor b 'are' the list, they only point, or reference the created object