The string memory resident mechanism is illustrated in detail
// Memory - resident mechanism for strings public static void Test() { // When multiple string variables contain the same actual value of the string, //CLR Instead of allocating memory to them repeatedly, you might want them all to point to the same thing 1 Two instances of a string object. String s1 = "Hello"; String s2 = "Hello"; bool same = (object)s1 == (object)s2;// To compare 1 Under the s1 and s2 Whether the same 1 A reference Console.WriteLine(same); // This place is called true: Said the same 1 A reference No new memory space is allocated /* * We know, String Class has a lot of special things about it 1 Is that it is "not going to change." (immutable) . * This shows that every time we are right 1 a String Object to operate on ( Such as using Trim . Replace Methods such as ) . * It's not really true about this String The instance of the object is modified and returned instead 1 A new one String Object instance as the result of operation execution. * String Object instance 1 After generation, it will not be changed until death ! */ /* About resident pooling: it is a string that maintains which literals, but does not maintain the following type */ StringBuilder sb = new StringBuilder(); sb.Append("Hel").Append("lo"); String s3 = "Hello"; String s4 = sb.ToString(); // It's the same value but it's not the same value 1 A reference bool same2 = ((object)s4 == (object)s3); Console.WriteLine(same2); /* Let the programmer force it CLR Check the resident pool ; See if you have the same string */ StringBuilder sb2 = new StringBuilder(); sb2.Append("He").Append("llo"); string s5 = "Hello"; string s6 = String.Intern(sb2.ToString()); bool same3 = (object)s5 == (object)s6; Console.WriteLine(same3); }