Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,
I have a big confusion regarding how strings are treated in c#.

  • when we declare a variable
    C#
    string str1 
    Is it treated as Reference Type or Value Type?
  • If awnser to question one is Reference Type then how are we able to compare two string like this
    C#
    if(str1==str2)

    Here the string is working like value types. How come???
  • Similarly in other situtions like an array of strings behaves exactly like value type rather than reference type. Then why do we call strings are reference type???
Posted
Comments
Sergey Alexandrovich Kryukov 7-Feb-12 1:58am    
There is a little flaw in your logic. However, this topic is not simple at all and pretty interesting. Please see my answer. So, I voted 4 for the question, and believe me, I put such a high mark for a question quite rarely.
--SA

The quick answer is: the type System.String is the class, that is, a reference type closely simulating value type semantics.

Comparison of reference type by value (your second question) is not a miracle.

You can easily implement such thing in your own class. This is done by implementing of the custom operators "==" and "!=". For referential equality, you should use the static (and of course, non-virtual, not to be overridden) method System.Object.ReferenceEquals. You need to use this method when overriding equality. The algorithm is this: 1) check up if one or both operands for null and return false and true for equal (use the method mentioned above); 2) if not are non-null, perform semantic test (in case of string, by content).

But, in case of System.String, it's much more complex and interesting! The referentially different strings of the same content can reference the exact same physical memory, and assignment of the value to the string generally changes its referential identity; this is done to reuse memory used by different string in a way transparent to the user. This is called "string interning".

Please see:
http://en.wikipedia.org/wiki/String_interning[^];
see this method in MSDN and code samples explaining string behavior:
http://msdn.microsoft.com/en-us/library/system.string.intern.aspx[^].

As to your third question: still, string is a reference type. I explained it above.

Interesting, isn't it?

—SA
 
Share this answer
 
Comments
Abhinav S 7-Feb-12 2:04am    
Right. My 5. I did not mention string interning as that was not what the OP asked. However, no harm in providing additional information.
Sergey Alexandrovich Kryukov 7-Feb-12 2:22am    
Thank you, Abhinav. I would like to note: this information is not "additional", it is explaining. Please see the code sample OP added in the comment to your question. You cannot explain this code unless you consider interning (even if you don't use the form). The trick .NET runtime does it to look for a value in intern poop (which can be optimized somehow) or creating a brand new string object.
--SA
Although string is a reference type, the equality operators (== and !=) are defined to compare the values of string objects.
Thus the == works on strings. This may[^] help you as well.

An array of strings is not a value type - so I'm not sure what scenario you could be talking about.
 
Share this answer
 
Comments
Mateen Khan 7-Feb-12 1:57am    
Hi abhinav,
I want to know what special have been done with these equality operators for string to compare the values of string objects.Have they overloaded these operators to compare two strings?
Also lets see this below code:
to compare the values of string objects.

string s1 = "Hello";
string s2 = s1;
s2 = "S1Changes";

MessageBox.Show(s1);
MessageBox.Show(s2);
The output to this will be: Hello
S1Changes
Now here since strings are reference types then why there is no change in the value of S1?

Also coming on to arrays:
I created an array of objects of some class and did array.Clone() and then changed the value of cloned object. This changes the value of original object too but when i repeated the same for string array there was no change in the value of orginal array object.Please if you can help why this is hapenning?
Sergey Alexandrovich Kryukov 7-Feb-12 2:01am    
The answer is in "string interning". Please see my answer. As soon as you assign new value to one of the strings, runtime detects that two strings are referentially identical and change the reference in the second variable. This is a very sly technique used to simulate value semantic in reference type.

Are you getting the picture? Please see my answer.
--SA
Sergey Alexandrovich Kryukov 7-Feb-12 2:04am    
And array are not like strings. They are simple, nothing equivalent to interning.
--SA
Sergey Alexandrovich Kryukov 7-Feb-12 2:27am    
By the way, I appreciate your code sample and pointing out the paradox. Very good!
I hope I explained how this paradox is resolved in my answer.
--SA
Sergey Alexandrovich Kryukov 7-Feb-12 1:59am    
Abhinav, this is only a half of truth. You are missing a big topic called "string interning". Please see my answer and links, I think for you it will be useful or interesting to know, too.
--SA
string is reference type but really special one. When you do operation of both string values like in your example
C = A, there is nothing in this operation simular to normal class behavior. So C doesn't have a reference of A. In that operation, a new string class is created and the value from string A is copied to string C. String C and String A are pointing in completly different memory address. That is way, when you have many string operation, for example loop and in every iteration you have a string manipulation, because of new string class generation, that operation is performance leak. In that situation it is preffered to use StringBuilder. Why, because StringBuilder doesn't create new string for any string operation

more information in : http://programmingincsharp.com/is-a-c-string-a-value-type-or-a-reference-type/[^]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 7-Feb-12 2:03am    
This article and you explanation are pretty good but not quite complete, because both do not explain the technique called "string interning". Please see my answer.
--SA

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