About the mechanism of string resident, those who know it will surely think it is very simple, but I believe a lot of people will be confused about it. An interesting Sample is provided before you begin to deploy the string:
Code Snip:
Static void Main (string [] args)
{
String str1 = "ABCD1234 ";
String str2 = "ABCD1234 ";
String str3 = "ABCD ";
String str4 = "1234 ";
String str5 = "ABCD"> "1234 ";
String str6 = "ABCD" + str4;
String str7 = str3 + str4;
Console. WriteLine ("string str1 =" ABCD1234 ";");
Console. WriteLine ("string str2 =" ABCD1234 ";");
Console. WriteLine ("string str3 =" ABCD ";");
Console. WriteLine ("string str4 =" 1234 ";");
Console. WriteLine ("string str5 =" ABCD "+" 1234 ";");
Console. WriteLine ("string str6 =" ABCD "+ str4 ;");
Console. WriteLine ("string str7 = str3 + str4 ;");
Console. WriteLine ("object. ReferenceEquals (str1, str2) = {0}", object. ReferenceEquals (str1, str2 ));
Console. WriteLine ("object. ReferenceEquals (str1," ABCD1234 ") = {0}", object. ReferenceEquals (str1, "ABCD1234 "));
Console. WriteLine ("object. ReferenceEquals (str1, str5) = {0}", object. ReferenceEquals (str1, str5 ));
Console. WriteLine ("object. ReferenceEquals (str1, str6) = {0}", object. ReferenceEquals (str1, str6 ));
Console. WriteLine ("object. ReferenceEquals (str1, str7) = {0}", object. ReferenceEquals (str1, str7 ));
Console. WriteLine ("object. ReferenceEquals (str1, string. Intern (str6) = {0}", object. ReferenceEquals (str1, string. Intern (str6 )));
Console. WriteLine ("object. ReferenceEquals (str1, string. Intern (str7) = {0}", object. ReferenceEquals (str1, string. Intern (str7 )));
}
The output result is as follows:
Next we will analyze the code one by one:
First, we created two identical strings (ABCD1234) and assigned them two character creation variables, str1 and str2. Then they are passed to object. ReferenceEquals. We know that object. ReferenceEquals is used to determine whether two variables have the same reference. In other words, if the two variables reference the same memory, True is returned. Otherwise, False is returned.
String str1 = "ABCD1234 ";
String str2 = "ABCD1234 ";
Object. ReferenceEquals (str1, str2) = True;
Object. ReferenceEquals (str1, "ABCD1234") = True;
What makes us strange is that when we create two variables of the reference type respectively -- string is the reference type. It is reasonable to say that CLR will allocate two segments of memory to them in the Managed Heap, and they cannot have the same reference. But why does the object. ReferenceEquals method return True. The second comparison -- a string variable and a string ("ABCD1234";) with the same content are directly compared. According to our general understanding of CLR memory allocation, the CLR first allocates memory for this string ("ABCD1234") in the managed heap, and then passes the corresponding reference to the object. the ReferenceEquals method (because the string allocated to the managed heap is not referenced by any variable, it will be reclaimed when garbage collection is executed), so True should not be returned in any case.
Let's leave the problem to the end and analyze our Sample. The above comparison is made between string variables and between variables and strings. If we compare a string variable with a dynamically created string (connect two strings through + Operator, what are the results? Let's take a look at the following pseudo-code Demonstration:
String str3 = "ABCD ";
String str4 = "1234 ";
String str5 = "ABCD"> "1234 ";
String str6 = "ABCD" + str4;
String str7 = str3 + str4;
Object. ReferenceEquals (str1, str5) = True
Object. ReferenceEquals (str1, str6) = False
Object. ReferenceEquals (str1, str7) = False
In the preceding example, three string variables (str5, str6, str7) are created in three different ways: string + string; string + variable; variable + variable. Then compare it with the created variable (str1) that has the same string "value" with them. We are also surprised that the first returns True, and the last two returns False. With these questions, let's take a look at the special type of string.