Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
I create two variable with object type , i initialized variable "d" ;then i assign
variable "d" to variable "h".finally changed value variable "d",but variable "h" do not change while object is reference type and "h" must be change with new value?
C#
object d = 5;
object h = d;
d = 9;        //h variable do not change!!
Posted
Updated 29-Jan-12 6:41am
v4
Comments
[no name] 29-Jan-12 13:52pm    
Since my solution is incorrect, I'm deleting it. Apologies :)

The type of your variables is a reference type System.Object (and object is an alias in language), a common base of all types. What happens with primitive types like integers?

Working with reference types, you store two objects: the variable itself stored in a variable, and the reference points to some object stored in heap. When you assign, reference is changed, object is not.

A variable has some compile-time type which never changes and is defined only by a declaration. Its compile-time type never changes, but run-time type can be changed during run time. All variable of the type System.Object are assignment-compatible with any type (on the right of the assignment operator), but non-reference types are boxes, see http://en.wikipedia.org/wiki/Boxing_%28computer_science%29#Boxing[^], http://msdn.microsoft.com/en-us/library/yz2be5wk.aspx[^].

Here is what happened:

In fist line, a boxed form of 5 is created and assigned to d.

In second line, h is done referentially equal to d. You obtain one single boxed form of 5 referenced by the two variables.

In third line, a boxed form of 9 is created and assigned to d. It created a brand new reference, not modified a previously created boxed form.

Assignment to variable of reference types in general case change the reference, not the value of anything.

The boxed object is immutable from the standpoint of the language, but in principle you can modify it using Reflection.

—SA
 
Share this answer
 
v8
Comments
Wonde Tadesse 29-Jan-12 13:18pm    
5+
Sergey Alexandrovich Kryukov 29-Jan-12 13:47pm    
Thank you, Wonde.
By the way, nobody voted at this time, you forgot.
--SA
Sander Rossel 29-Jan-12 13:53pm    
My 5! I was wondering about what happened exactly when I read the question. Good explanation.
Sergey Alexandrovich Kryukov 29-Jan-12 13:57pm    
Thank you, Naerling.
How are you doing?
--SA
Sander Rossel 29-Jan-12 14:41pm    
Pretty well, thanks :)
I've been playing a lot of Skyrim last month, so I haven't been on CP a lot. I'm back on earth though. I've currently got a lot of work, even in weekends... But I'm trying to be on CP a bit more often, so you'll see me around.
Are you still doing good too? Still master of the QA I see!
I have to disagree with the answer by J_N above: value Types are boxed, and unboxed: see: [^].

A reference Type remains a reference Type: consider this simple example:
C#
public class SomeClass
{
    public string someString { get; set; }
}
Now execute this code:
SomeClass sClass = new SomeClass();

SomeClass dClass = sClass;

sClass.someString = "hello";

dClass.someString = "goodbye";
And set a break-point right after the last statement in the code.

You will see that changing the string value of the Property 'someString in 'dClass which has been set equal to the instance of 'sClass: also changes the string value of the Property 'someString in 'sClass.

This is because both the instance of 'sClass, and the pointer to the instance of 'sClass held in 'dClass: are both references to the same underlying Object.
 
Share this answer
 
v2
Comments
Wonde Tadesse 29-Jan-12 13:18pm    
5+
Sergey Alexandrovich Kryukov 29-Jan-12 13:54pm    
I have a mixed feeling: you have a number of good points, but they and your examples do not address the confusion of OP at all, because you only work with System.String, which is a reference type.

To be more accurate, a note on your first statement: it's correct, but there is no any unboxing in the OP's code, only boxing.

So, you post is kind of out of topic. But maybe reading it just would be a useful exercise for OP in near future. So... I voted 4 this time.
--SA
BillWoodruff 29-Jan-12 16:08pm    
I'd really appreciate knowing from the evaluator who down-voted this answer -16 points, what the reason was for that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900