Example:
- class Class1
- {
- static void StrChange(string str)
- {
- str = "hellow";
- }
- static void Main()
- {
- string str = "123";//declare a string variable
- StrChange(str);//Called a method
- Console.WriteLine(str);//output the value of this string variable str.
- }
- }
like that, string is value types or referenced types in the end,someone thought it is value types, if it is value types,the result is right. but the define of string is referenced types,like this said,the result should is "hello"?
The key of this problem is that C# string types is a special referenced types,the instance is readOnly, we have to know the difference of the grammar and realization. In the C#'s grammar,
?static void StrChange(string str) //transfer the value
?static void StrChange(ref string str) //transfer the refrenced
so static void StrChange(string str) that the value is transfered, after we changed it in this function, the variable and out variable point same memory, so that's referenced of transfering , but when we changed the value in this function, it will be assigned new memory to save the value.
So the types of String is
referenced , and the value is immutable. when we change
it(ToUpper(),toLower() etc..),in fact it will create a new string,That's
also why don't use string when we link too big char,we used
StringBuilder.
my english is not very well, hope the answered is useful to you.
