471,334 Members | 1,458 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,334 software developers and data experts.

Referenced Object

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.
Aug 12 '08 #1
6 1481
On Tue, 12 Aug 2008 08:40:18 -0700, sandy <sa******@gmail.comwrote:
[...]
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?
Because "a1" and "a2" are variables, not the objects themselves. The
variables _reference_ the objects; changes to the variables change only
what they reference. Those changes can't affect other variables.

Jon Skeet has an article that's not really about value vs. reference types
per se, but I feel it does a pretty good job of describing this issue
anyway:
http://yoda.arachsys.com/csharp/parameters.html

You may find that helpful in elaborating on the question.

Pete
Aug 12 '08 #2
On Aug 12, 11:40*am, sandy <sandy...@gmail.comwrote:
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.
Aug 12 '08 #3
>>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.
>>a1 = null;<<
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

*** Sent via Developersdex http://www.developersdex.com ***
Aug 12 '08 #4
sandy wrote:
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.

--
Göran Andersson
_____
http://www.guffa.com
Aug 13 '08 #5
On Aug 13, 5:57*am, Göran Andersson <gu...@guffa.comwrote:
sandy wrote:
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.

--
Göran Andersson
_____http://www.guffa.com
Thanks for the clarification. I think I get it now. I still have the C
concepts in my mind while coding for C#.
Aug 13 '08 #6
On Aug 13, 1:30*pm, sandy <sandy...@gmail.comwrote:
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
Aug 13 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Phil Jones | last post: by
12 posts views Thread by Joe | last post: by
6 posts views Thread by Jonas Huckestein | last post: by
4 posts views Thread by Christian Joergensen | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.