Referenced Object

S

sandy

Hello,

My understanding about objects is that they are reference types and
not value types but a simple code seems to defy my whole
understanding.

I wrote a simple console app:

class Program
{
static void Main(string[] args)
{
A a1 = new A();
a1.i = 1;
a1.j = 2;

A a2 = a1;

a1 = null;

Console.WriteLine(a2.i);
}
}

class A
{
public int i;
public int j;
}

My understanding is a2 and a1 should both both to the same location in
the heap. Once a1 is made null, a2 should also become null but it does
not happen?

Why? Thats the question.
 
I

Ignacio Machin ( .NET/ C# MVP )

Hello,

My understanding about objects is that they are reference types and
not value types but a simple code seems to defy my whole
understanding.

I wrote a simple console app:

class Program
    {
        static void Main(string[] args)
        {
            A a1 = new A();
            a1.i = 1;
            a1.j = 2;

            A a2 = a1;

            a1 = null;

            Console.WriteLine(a2.i);
        }
    }

    class A
    {
        public int i;
        public int j;
    }

My understanding is a2 and a1 should both both to the same location in
the heap. Once a1 is made null, a2 should also become null but it does
not happen?

Why? Thats the question.

a2 and a1 are simply holding a reference to the object (think of it
like they are holding the address of the object) , when you set a1 to
null they simply makes references to different objetcs (or different
addresses)

As a matter of fact the "real" object will be kept in memory while at
least one variable makes reference of him. After the last variable
does not reference it, the object's memory can be collected.
 
J

Jeff Louie

A a2 = a1;<<

There is no call to a copy constructor here in C#. The reference
variable a2 is
assigned a reference to an existing object at this point. No new object
is created
by this call.

At this point the variables a2 and a1 both hold references to a single
instance of
Class A.

At this point the variable a1 no longer holds a reference to the single
instance of
Class A.
But the variable a2 still holds a valid reference to the instance of
Class A.

Hope that helps,
Jeff
 
G

Göran Andersson

sandy said:
Hello,

My understanding about objects is that they are reference types and
not value types but a simple code seems to defy my whole
understanding.

I wrote a simple console app:

class Program
{
static void Main(string[] args)
{
A a1 = new A();
a1.i = 1;
a1.j = 2;

A a2 = a1;

a1 = null;

Console.WriteLine(a2.i);
}
}

class A
{
public int i;
public int j;
}

My understanding is a2 and a1 should both both to the same location in
the heap. Once a1 is made null, a2 should also become null but it does
not happen?

Why? Thats the question.

Because you are not setting the object to null, you are setting the
reference to null.

The a1 and a2 variables are references pointing to the same object.
Changing the object through either reference changes the same object,
but assigning a different reference (in this case null) to one variable
neither changes the object nor the other variable.
 
S

sandy

sandy said:
My understanding about objects is that they are reference types and
not value types but a simple code seems to defy my whole
understanding.
I wrote a simple console app:
class Program
    {
        static void Main(string[] args)
        {
            A a1 = new A();
            a1.i = 1;
            a1.j = 2;
            A a2 = a1;
            a1 = null;
            Console.WriteLine(a2.i);
        }
    }
    class A
    {
        public int i;
        public int j;
    }
My understanding is a2 and a1 should both both to the same location in
the heap. Once a1 is made null, a2 should also become null but it does
not happen?
Why? Thats the question.

Because you are not setting the object to null, you are setting the
reference to null.

The a1 and a2 variables are references pointing to the same object.
Changing the object through either reference changes the same object,
but assigning a different reference (in this case null) to one variable
neither changes the object nor the other variable.

Thanks for the clarification. I think I get it now. I still have the C
concepts in my mind while coding for C#.
 
J

Jon Skeet [C# MVP]

Thanks for the clarification. I think I get it now. I still have the C
concepts in my mind while coding for C#.

When you've still got a C mindset, think that roughly "C pointer" ~=
"C# reference" except you can't do pointer arithmentic.

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top