Search code examples
c#stringvalue-typereference-type

why string behaves as value type even though it is a reference type in c#


Possible Duplicate:
In C#, why is String a reference type that behaves like a value type?

I know string is a reference type since string can be very large and stack is only 1 mb . But programatically while coding i see it behaves like value type for eg

string func_name(string streg)

{    
streg="hello";

return streg; 

}

-------------

string str="hi";

str= func_name(str);

now str gets the value hello ?

why so ? it bahaves exactly like value type here.


Solution

  • Because it was decided that the string type would be immutable, but strings can still be very large.

    You can read why here:

    Why .NET String is immutable?

    And another question similar to yours:

    In C#, why is String a reference type that behaves like a value type?