Question on Assignment

  • Thread starter Thread starter Bruce Vander Werf
  • Start date Start date
B

Bruce Vander Werf

Because Rectangle is a value type, I know I can do this:

Rectangle rect1, rect2;

rect1.Top = 34;
rect2 = rect1; //copies members
rect1.Top = 46; //does not affect rect2

But what about an object that provides static members, like IQAddress?

IQAdress addr1, addr2;

addr1 = IPAddress.None; //creates addr1 on heap?
addr2 = IPAddress.Broadcast; //creates addr2 on heap?
addr2 = addr1; //copying a pointer?
addr1.ScopeId = 5; //also changes addr2?
addr2 = IPAddress.Loopback; //creates a new pointer on the heap?

I would appreciate any insight on this.

--Bruce
 
Bruce Vander Werf said:
Because Rectangle is a value type, I know I can do this:

Rectangle rect1, rect2;

rect1.Top = 34;
rect2 = rect1; //copies members
rect1.Top = 46; //does not affect rect2

But what about an object that provides static members, like IQAddress?

IQAdress addr1, addr2;

addr1 = IPAddress.None; //creates addr1 on heap?
addr2 = IPAddress.Broadcast; //creates addr2 on heap?
addr2 = addr1; //copying a pointer?
addr1.ScopeId = 5; //also changes addr2?
addr2 = IPAddress.Loopback; //creates a new pointer on the heap?

I would appreciate any insight on this.

Have a look at http://www.pobox.com/~skeet/csharp/memory.html and see
if that helps you.
 
Nice article, but it really doesn't "address" the subject of
assignment, which is where my questions lie.
 
Bruce,

Here is what happens when you make your assignments:

addr1 = IPAddress.None; //creates addr1 on heap?

addr1 is assigned a reference to the object returned by the static None
property. Now, because it is a property, it is possible that it returns a
reference to a single instance that is stored in a static variable
(unlikely, you will see why soon), or that it returns a copy of what is
stored statically. Regardless, the object that is returned is on the heap,
as are all reference types. What is returned is a reference to that object
on the heap.

addr2 = IPAddress.Broadcast; //creates addr2 on heap?

Everything said for addr1 applies here.

addr2 = addr1; //copying a pointer?

You are copying a reference, not a pointer. There are differences but
you have the general idea.

addr1.ScopeId = 5; //also changes addr2?

Yes, in this case, because addr1 and addr2 point to the same reference,
modifying a property the object that one references will affect the value if
the same property is accessed when a different variable with the same
reference is accessed. This is probably why the static properties on the
IPAddress class don't return stored references, but rather, references to
copies of the objects that represent None, Broadcast, etc, etc.

addr2 = IPAddress.Loopback; //creates a new pointer on the heap?

addr2 is assigned a different reference, assuming that the static
Loopback property doesn't return the same value as the Broadcast or None.

Hope this helps.
 
Bruce Vander Werf said:
Nice article, but it really doesn't "address" the subject of
assignment, which is where my questions lie.

But assignment is just copying the value of one expression into a
variable. If you know what the value of the expression is (a reference
or not) then the assignment part is easy - and the article describes
where the variables themselves are stored.
 
addr1 is assigned a reference to the object returned by the static
None
property. Now, because it is a property, it is possible that it returns a

But Nicholas, the IPAddress.* members are readonly fields, not properties.
What happens when you assign something to the value of a readonly field, and
then change the value through the other reference?

Chris
 
Chris Capel said:
But Nicholas, the IPAddress.* members are readonly fields, not properties.
What happens when you assign something to the value of a readonly field, and
then change the value through the other reference?

A value is just a value. It doesn't matter how you got it - the value
of IPAddress.Any is a reference just like any other reference.
 
But Nicholas, the IPAddress.* members are readonly fields, not
properties.
A value is just a value. It doesn't matter how you got it - the value
of IPAddress.Any is a reference just like any other reference.

But doesn't that mean that you could change the referent of IPAddress.*,
thus defeating the purpose of making it readonly? For instance, if you
changed the ScopeID of IPAddress.Broadcast, another part of your application
(maybe even another application altogether) would get the changed ScopeID.
What's the scope of a static variable in a framework class? Per-application
domain?

Chris
 
Chris Capel said:
But doesn't that mean that you could change the referent of IPAddress.*,
thus defeating the purpose of making it readonly?
Yes.

For instance, if you
changed the ScopeID of IPAddress.Broadcast, another part of your application
(maybe even another application altogether) would get the changed ScopeID.

Well - using ScopeId on IPAddress.Broadcast gives an exception, but you
can change the Address property of IPAddress.Broadcast, yes. Personally
I think the IPAddress.* fields should have been readonly properties
that returned clones of the original version, so that people could
screw them up for themselves if they wanted, but it not affect anything
else.
What's the scope of a static variable in a framework class? Per-application
domain?

I believe so.
 
Back
Top