472,983 Members | 2,247 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,983 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 1548
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Phil Jones | last post by:
Is there a way to determine if an object is being referenced by any other objects? This is what the GC does to determine whether an object should be properly destroyed. I was wondering if this...
1
by: Jean Stax | last post by:
Hi ! I created a sample library project. In my second project I reference this library and make the following call, which returns "undefined value": Type myType =...
4
by: Jimi | last post by:
Given a C# project file path, can I use reflection to get all the assemblies referenced by the project? e.g., I know the path of a C# project, say, "c:\SomeProject.csproj", and I want to load...
1
by: Jimi | last post by:
(Sorry for the repost, but I thought maybe I'd have better luck this time) I'm working on a utility to provide an overview of one's .NET projects and one of the things I want to display is...
12
by: Joe | last post by:
I might be overworked so please excuse this stupid question... Say I do the following: DataTable table = new DataTable(); myDataAdaptor.Fill(table); dataGrid1.DataSource = table;
1
by: veronique.brenda | last post by:
Hi, I have a macro on Ms Access 2000 with an action: SetValue; Item: !. and Expression:Yes. I have an OpenForm action just before this action, so definitely the form is opened when the...
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
6
by: Jonas Huckestein | last post by:
hello, somehow i can't figure out, how to overload the operator for a referenced object. if i have class MyClass { int operator(int i) { return 1; }; };
4
by: Christian Joergensen | last post by:
Hello I stumpled upon this "feature" during my work tonight, and found it a bit confusing: .... class C: .... foobar = 42 .... .... <class __main__.C at 0xb7cf735c>
1
by: TomGo | last post by:
Hi All I want to read out the properties of a field by the instance, which is referenced by the corresponding field. I want to read out the attributes. In a class I wrote the following: ...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.