Status
Not open for further replies.

hariharan

Journeyman
int iNum = 16; // declaring a value type int
object oVal1 = iNum; // boxing the value type int
object oVal2; // creating a new object reference on the stack
oVal2 = oVal1; // assigning the object reference to another reference
oVal1 = 32; // changing the value of the boxed reference
System.Console.Writeln(oVal1);
System.Console.Writeln(oVal2); //printing the values


Now my ques is

Why does changing the value of the reference of one object does not change the value of the other object as both are references pointing to the same location?

I know that changing the value of a boxed reference does not change its value in the value type.. but when reference typed values are changed should they be reflected in other references also?
 
Its simple. Everytime you box a value to an object, a new memory location is allocated for it. So for your example, something like this is going on:

Code:
int iNum = 16;          // iNum @ location XX ->value 16
object oVal1 = iNum;    // oVal1 @ location YY ->value 16
object oVal2;          // oVal2 @ location ZZ ->value null
oVal2 = oVal1;         // oVal2 @ location YY ->value 16
oVal1 = 32;           // oVal1 @ location AA -> value 32

System.Console.Writeln(oVal1);   // Prints oVal1@location AA=32
System.Console.Writeln(oVal2);  // Prints oVal2@location YY=16

Here XX, YY, ZZ and AA are assumed memory locations of these variables.

I hope this had clear things up for you. Correct me if i am wrong somewhere.

:)
 
OP
H

hariharan

Journeyman
thanks 4 u time.. both of u guys..

oVal1 = 32;
in this line a new object is created and assigned to oVal1. oVal2 still pointing to the old object so these two values are different.

this new object for oVal1 is created only in the case of boxing. isn't it?


Here XX, YY, ZZ and AA are assumed memory locations of these variables.

Is it possible to see the memory location actually assigned?

Are the memory locations different for the objects every time they are assigned a value?

Or is it the case the memory locations are different for the objects only when they are assigned a value which needs to be boxed.
 
Well you cant see the actual memory location even if you mark this code as unsafe. That is because object is a managed data type, and you cant get address of an managed data type. If it would be an int or something then after putting this code in an unsafe{ } block you can take address of a variable same as you do it in C ie. by using & operator.

No, when an regular object is assigned to another object, it always points to the second objects memory address. This is evident from these lines:

Code:
object oVal1 = iNum;    // oVal1 @ location YY ->value 16
object oVal2;          // oVal2 @ location ZZ ->value null
oVal2 = oVal1;         // oVal2 @ location YY ->value 16

First oVal2 was at ZZ, then after doing oVal2=oVal1, oVal2 is at YY, the location of oVal1. This is normal behavior of objects.

But any statement that causes boxing, a new memory is given to object. So when oVal2=32 was executed, a value type (int) is assigned to a reference type (object) so oVal2 is given a new memory.

Hope this clear things up for you :)
 
Status
Not open for further replies.
Top Bottom