Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
4.33/5 (3 votes)
See more:
Can explain me how the string is refence type?
Posted

While it's true that not all value types have to be stored on the stack, it is true that a string has to be stored on the heap (this is often given as the reason - string can be huge so they couldn't be stored on the stack).

In theory, a string could well have been a value type, but the value would simply have been the reference to the string. When .NET was first developed, structs were seriously inefficient and the language designers decided to follow the Java convention whereby a string was not a native type, but rather a reference type.

One other point to consider - if a string were a value type then converting it to an object would require a very inefficient boxing operation. Have a look at this[^] article to understand why this would be bad.
 
Share this answer
 
String is reference type not value type and it is stored on the heap whereas value type is stored on stack.
String is a reference type as it knows the size of the reference but not of the data and hence data size is not predefined.
Strings are immutable for the sake of memory management where they resemble the value types.
So String are reference type in all other aspects except being immutable and StringBuilder is the mutable reference type.
 
Share this answer
 
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.

Now for question, why string is not value type and it's reference type.
When you have let's say int value, Int type has defined lenght and that is 4 byte. And for every value type you know the exact memory space that will take. But for string you don't know that. It will be stupid to have a string type in wich you should always define how long it is. And if you want to put in that string variable a value with bigger lenght that string is defined, that will be disallowed.
Why is string a special class. It is special because you have a reference class, but the way of using it, looks like a value type. So, that's why you do operation like:
string C = A, and not string C = new string(A);
You don't have to instance string class because .NET framework do that for you when you first time set it's value. If you not set it's value string has value of null like any other reference type. Every language platform has a special treatment for string type. .NET is not exclusion from that too.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-Jul-11 2:46am    
Best answer so far, my 5.
I've added information important for understanding the nature of strings, please see my answer.
--SA
All objects in .Net are reference types.
 
Share this answer
 
string is a reference type.
It can't be a value-type, as value-types need a known size for the stack etc. As a reference-type, the size of the reference is known in advance, even if the size of the string isn't.

It behaves like you expect a value-type to behave because it is immutable; i.e. it doesn't change once created. But there are lots of other immutable reference-types. Delegate instances, for example.
*=except for inside StringBuilder, but you never see it while it is doing this.

for more details refer this:

http://www.yoda.arachsys.com/csharp/strings.html[^]

hope this helps :)
 
Share this answer
 
v2
The type is a reference type thoroughly imitating value semantic.

You will understand the very special nature of the reference type System.String if you read about interning.

See the property System.String.IsInterned and the method System.String.Intern, http://msdn.microsoft.com/en-us/library/system.string.isinterned.aspx[^], http://msdn.microsoft.com/en-us/library/system.string.intern.aspx[^].

One important thing about having immutable string and interning is thread-safety.

—SA
 
Share this answer
 
Comments
CS1401 2-Jul-11 2:59am    
yap good..
Sergey Alexandrovich Kryukov 2-Jul-11 3:14am    
Thank you.
--SA
sting are reference type.
object also reference type
integer is as value type
 
Share this answer
 
definitely string is a reference type in .net
 
Share this answer
 
In .Net Framework Strings are immutable reference types. All .net datatypes has default size except string and user type. So String is a Reference type, because its does not have default allocation size. The maximum string length will depend on your machine's architecture and memory allocation situation, but the String.Length property is a 32-bit integer, so there's an effective upper bound of Int32.MaxValue. More about......Is string a value type or a reference type

Mark
 
Share this answer
 
Comments
CHill60 1-Jun-15 4:33am    
Question is over 3 years old and fully answered. Posting links to what is essentially nothing more than an "interview questions - 4 - U" website may be thought to be spam. I advise not doing it.

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