Reference type in C#
Solution 1:
Both person
and person2
are references, to the same object. But these are different references. So when you are running
person2 = null;
you are changing only reference person2
, leaving reference person
and the corresponding object unchanged.
I guess the best way to explain this is with a simplified illustration. Here is how the situation looked like before person2 = null
:
And here is the picture after the null assignment:
As you can see, on the second picture person2
references nothing (or null
, strictly speaking, since reference nothing and reference to null
are different conditions, see comment by Rune FS), while person
still references an existing object.
Solution 2:
Consider person1
and person2
as pointers to some location in storage. In the first step, only person1
is holding the address of the object from storage and later person2
is holding the address of memory location of object from storage. Later when you assign null
to person2
, person1
stays unaffected. That is why you see the result.
You may read: Value vs Reference Types from Joseph Albahari
With reference types, however, an object is created in memory, and then handled through a separate reference—rather like a pointer.
I will try to depict the same concept using the following diagram.
Created a new object of type person and the person1
reference (pointer) is pointing to the memory location in storage.
Created a new reference(pointer) person2
which is pointing to the same in storage.
Changed the object property Name to new value, through person2
since both references are pointing to the same object,Console.WriteLine(person1.Name);
outputs Shahrooz
.
After assigning null
to person2
reference, it will be pointing to nothing, but person1
is still holding the reference to the object.
(Finally for memory management you should see The Stack Is An Implementation Detail, Part One and The Stack Is An Implementation Detail, Part Two from Eric Lippert)
Solution 3:
You have changed person2
to reference null
, but person1
isn't referencing there.
What I mean is that if we look at person2
and person1
before the assignment then both reference the same object. Then you assign person2 = null
, so person 2 is now referencing a different type. It did not delete the object that person2
was referenced to.
I've created this gif to illustrate it:
Solution 4:
Because you've set the reference to null
.
When you set a reference to null
, the reference itself is null
.. not the object it references.
Think of them as a variable that holds an offset from 0. person
has the value 120. person2
has the value 120. The data at offset 120 is the Person
object. When you do this:
person2 = null;
..you're effectively saying, person2 = 0;
. However, person
still has the value 120.
Solution 5:
Both person
and person2
point to the same object. Therefore when you change the name of either one, both will get changed (since they point to the same structure in memory).
But when you set person2
to null
, you make person2
into a null pointer, so that is does not point to the same object as person
anymore. It wont do anything to the object itself to destroy it, and since person
still points/references the object it wont get killed by garbage collection either.
If you also set person = null
, and you have no other references to the object, it will eventually be removed by the garbage collector.