First look at the code:
int a = 1; int b = A; A = 2; BOOL Valorref = b = = 2 ;//false;
You can see that the int type is a value type because b does not follow a change.
Then look at the following code:
String str11 = "QA"; string str22 = Str11; Str11 = "Qaz"; BOOL ValOrRef2 = Str22 = = "Qaz"; False
The result is also false, so a string is also a value type, a.
Then look at the following code:
String str1 = "Qaz"; String str2 = "Qaz"; BOOL Refequ = object. ReferenceEquals (str1, str2); True
Refequ equals True, if the string is really value type, then how can refequ be true, because look at the following code:
int a = 1; int b = 1; BOOL Refequ2 = object. ReferenceEquals (A, b);//false
int is a value type, which is certain and declares 2 int variables.
This is paradoxical, how to explain, string looked like value type, but looked like reference type.
Microsoft:string is reference type. So how does the explanation look like value type?
Please remember 2 points:
1)A String object whose reference address is the same if the value is the same.
2)A String object whose reference address is unequal if the value is not equal.
Analyze the above code:
String str11 = "QA";//str11 points to memory Addressa, data is QA; string str22 = Str11;//str22 also points to memory Addressa str11 = "Qaz";// Str11 new point to the memory ADDRESSB, the data is Qaz bool ValOrRef2 = Str22 = = "Qaz";//false
Summarize:
One of the most notable features of string is its constant invariance : Once we have created a string and allocated a contiguous memory space on the managed heap, we will not be able to modify the string in any way to make it grow longer, shorter, Change the format. All of the strings returned for each operation of this string, and actually another string that was recreated, do not, in itself, produce any changes.
Postscript:
How efficient is String?
A string object is called an immutable (read-only) object , because once the object is created, the value of the object cannot be modified. Sometimes it seems to change, actually is a string after a special processing, each time the value is changed to create a new string object, the variable will point to the new object, and the original is still pointing to the original object, so it will not change. This is also the reason why string is inefficient.
The above is the content of c#string is value type or reference type, please pay attention to topic.alibabacloud.com (www.php.cn) for more information!