0

I have a doubt on how reference types actually work. I have a class Person with two properties Name and Age. I am creating an object of Person class (objPerson1), assigning some values to both the properties and assigning that object to another of type Person (objPerson2). Now the question is after assigning when I change the Name and Age property and print them both the object shares same Name and Age which is fine as they are reference type.But when I assign null value to object itself then other object doesn't get nullified.Below is the code

class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public static void Main(string[] args)
{
    Person objPerson1 = new Person();
    objPerson1.Name = "Tom";
    objPerson1.Age = 10;

    Person objPerson2 = objPerson1;
    objPerson2.Name = "Jerry";
    objPerson2.Age = 15;
    Console.WriteLine($" Person1-{objPerson1.Name},{objPerson1.Age} and Person2-{objPerson2.Name},{objPerson2.Age}");
    //Above line prints   Person1-Jerry,15 and Person2-Jerry,15
    //which is right as both are sharing same address.But when I wrote following code it confused me alot.
}

public static void Main(string[] args)
{
    Person objPerson1 = new Person();
    objPerson1.Name = "Tom";
    objPerson1.Age = 10;
    Person objPerson2 = objPerson1;
    objPerson2 = null;
    //After executing above line objPerson2 was null but objPerson1 were still having the values for Name and Age.
}

As they are reference type and both pointing to same address if I assign null to objPerson2 ,objPerson1 should also be null and vice-versa.Correct me if I'm wrong

Vishal Dhasal
  • 51
  • 1
  • 10
  • 4
    I have a document you may find useful: https://jonskeet.uk/csharp/references.html – Jon Skeet Jun 07 '19 at 11:12
  • Note that "But when I assign null value to object itself" suggests a problem: you don't assign an *object* a value (null or otherwise). You assign a *variable* with a value. See https://stackoverflow.com/questions/32010172/what-is-the-difference-between-a-variable-object-and-reference/32010236#32010236 for more details. – Jon Skeet Jun 07 '19 at 11:14
  • In short: person object will be created in the memory, both your variables will have a "links"/references to that memory. when you set to null - you remove a link. Object will stay in memory until garbage collector detect it has no links then destroys it. – Renatas M. Jun 07 '19 at 11:14
  • as both variables point to the same objects, changes to one are reflected in the other. However **assigning** some other instance - in your case `null` - is not the same as **modiyfing** an instance. So you don´t change the existing object, but simply re-reference another one - null here. – MakePeaceGreatAgain Jun 07 '19 at 11:16
  • Thank you guys now I understood how it actually works. – Vishal Dhasal Jun 07 '19 at 11:31

2 Answers2

4

A bit simplified, but hopefully sufficient for you to understand:

Person objPerson1 = new Person();

Heap: memory allocated for object

Stack: objPerson1 = address of the heap object

objPerson1.Name = "Tom";
objPerson1.Age = 10;

Heap: is being filled with values.

Stack: unchanged (still the same address)

Person objPerson2 = objPerson1;

Stack: another variable gets the same address

Heap: unchanged

objPerson2 = null;

Stack: the variable objPerson2 gets the value 0x00000000.

Note that objPerson1 still has the address of the heap and the object on the heap still exists. So objPerson1 still "works".

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
-1

objPerson2 is only a pointer to the memory allocated by the initialization of objPerson1. Assigning null to objPerson2 removes this pointer. objPerson1 still points to that memory therfore it holds it's value and does not become null once objPerson1 does.

Avi Meltser
  • 409
  • 4
  • 11
  • @Archer [is it not?](https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/) – Avi Meltser Jun 07 '19 at 11:25
  • @Archer really not sure what you meant by that comment? care to enlighten a bit? I believe you are pointing to *the* – Rahul Jun 07 '19 at 11:43