Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning one object to another of the same class

Please have a look at the code below:

class Parent{}

class Child : Parent
{
    public int field 
    {
        get; 
        set;
    }
}

class Program
{
    static void Main(string[] args)
    {
        Child child1 = new Child();
        Child child2 = child1;              //same memory address?

        child1.field = 12;
        child2.field = 14;

        Console.WriteLine(child1.field);    //14, as expected

        child1 = null;
        Console.WriteLine(child2.field);    //expected to crush: 'child1' is null so expected 'child2' to also be null

        Console.ReadLine();
    }
}

I expected this code to crash, but it does not not.

Why?

like image 818
Jacek Wojcik Avatar asked Apr 07 '14 13:04

Jacek Wojcik


4 Answers

That's not how references work.

Child child1 = new Child();

creates a reference to a new Child object

Child child2 = child1;  

Creates another reference that points to the same object that child1 does

child1 = null;

Sets the child1 reference to null (it no longer points to anything). It does not change the object it pointed to and does not affect child2. child2 still points to the same object it did before.

like image 78
D Stanley Avatar answered Oct 22 '22 18:10

D Stanley


No, you are just setting the reference value contained in the child1 variable to the null value.
The second object instance still contains the original reference value and still works

In a very simplified way, when you write

Child child1 = new Child();

the child1 instance is created in memory and a reference to it is returned with a value that we could represent as the value 1000. This value (the reference) is assigned to the child1 variable

Child child2 = child1;              

You assign the same reference value 1000 to the second variable

child1 = null;

You set the variable child1 the value zero (or whatever the implementers of null decide null to be), but the chidl2 still contains the reference value 1000.

Still having a valid reference to the memory area where the original object has been created correctly blocks the Garbage Collector from removing the object from memory and you could still safely use the second variable

like image 27
Steve Avatar answered Oct 22 '22 16:10

Steve


First you create a child object and assign it to the variable child1

Child child1 = new Child();

So you can think of it looking like this, child1 points to an instance of a Child object

child1 ---->  { Child object }

Then you do this:

Child child2 = child1;

This copies the reference so now both variable points to the same object:

child1 ---->  { Child object }
child2 ------------^

Then you do this:

child1 = null;

This removes from the variable child1 the reference to the instance of the Child object. The important point here is that the Child object itself is unaffected by this:

child1        { Child object }
child2 ------------^

So since child2 had a copy of the reference, it is unaffected too and still points to the instance of Child you created at the start.

like image 2
Matt Burland Avatar answered Oct 22 '22 18:10

Matt Burland


Yes when you assign a object to another object of the same class, the memory address or the reference is same.

But when you do child1=null;, the child2 still references the same object that it referenced during its initialization.

like image 1
Amit Joki Avatar answered Oct 22 '22 18:10

Amit Joki