Click here to Skip to main content
15,889,403 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
if object and string is refrence type,why in this code when p assigned with "sample" ,q not refrence to p and its value is null ?

string p = null;
       string q = p;
       p = "sample";    //p value is sample,q value is null
Posted
Comments
Abhinav S 13-Aug-11 6:08am    
Please have a look at OriginalGriff's answer. His answer is actually more accurate than mine with respect to your question.

What is happening in your example above is that p is now pointing to a new string object.
q still points to the old string, the string which was null.

In general, strings are reference types, but they behave slightly differently.
Although strings are reference type, they are immutable.

So the contents of a string object cannot be changed after the object is created, although the syntax makes it appear as if you can do this.

Read more about this here[^].
 
Share this answer
 
v2
Comments
Wendelius 13-Aug-11 5:47am    
My 5 very easy to agree :)
Abhinav S 13-Aug-11 5:47am    
Ha ha. Thank you.
Because "q" and "p" are both reference variables:
C#
string p = null;

Declares a variable called p, assigns it the value null
C#
string q = p;

Declares a separate variable called q, assignes it the value currently in p - i.e. null
C#
p = "sample";

Assigns p to refer to a string which contains "sample".
This does not alter q - it remains with the previous value of null

It's a bit like cars. If you have two cars, "p" and "q" and you drive "p" to the shops, would you expect "q" to follow you there on it's own?
 
Share this answer
 
Comments
Abhinav S 13-Aug-11 5:46am    
I don't quite agree with your answer.
If the string was a "normal" reference type, when p would change q would automatically change.

You can try that with an two variables point to the a object that has some properties. Change a property value and then access both the variable properties - you will find they are same.
OriginalGriff 13-Aug-11 5:52am    
Yes, I agree - but he isn't changing the object properties - he is changing the object being referenced.
Abhinav S 13-Aug-11 6:02am    
Ah I see your point. My 5. Your answer is better than mine.
I have corrected my answer.

Immutability has come into the picture here, but it has nothing to do with OP's question.
OriginalGriff 13-Aug-11 6:08am    
It's Saturday - nobodies brain is fully operational! :laugh:
Abhinav S 13-Aug-11 6:10am    
:).

I've requested the OP to look at your answer.
Since string is an immutable object reference variable you're not actually changing the value of an existing object but replacing the previous string with a new one.

Refer to: string (C# Reference)[^]
 
Share this answer
 
v3
Comments
Abhinav S 13-Aug-11 5:44am    
My 5. I posted the same answer 51 seconds before you.
:)
Wendelius 13-Aug-11 5:46am    
Thanks, Yes I noticed when coming here again that same kind answer has been posted almost at the same time :)
Abhinav S 13-Aug-11 6:06am    
I think we were both wrong. Have a look at OrignialGriff's answer.
Wendelius 13-Aug-11 7:13am    
Yes that's a good answer, made a slight enhancement. If we have a look at the hashcodes I think it explains it more thoroughly:

string p = "first";
// p.GetHashCode() = -1920740948

string q = p;
//q.GetHashCode() = -1920740948
//p.GetHashCode() = -1920740948

p = "second";
//q.GetHashCode() = -1920740948
//p.GetHashCode() = -792817032


So in the end q is still pointing to the original string but p is replaced with a new one.
Abhinav S 13-Aug-11 7:41am    
I did similar analysis with objects before replying to OriginalGriff.
He was right. :)

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