Click here to Skip to main content
15,908,661 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

the string class is sealed class.

any class type we are considering is ref type.

but why 'string' type under the value type.

please dont tel string is reference type.

Regards
raja
Posted

 
Share this answer
 
Because string type is immutable(You can't change the content of the object once it's been defined).

Ex.

string str = 'Hi';
str = str + ' all';

In second line, CLR create another object of string type, inspite of using existing string object.
 
Share this answer
 
 
Share this answer
 
It is confusing because string is a reference type that has (by the C# designers) been created to act in some cases as a value type.

So, for example, we can do string comparisons as s1 == s2 which actually compares the value of the strings, not the object references as it would with a 'true' reference object.

I believe they did it like that to be more intuitive - although IMHO it just leads to more confusion for beginners.
 
Share this answer
 

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