string s1 = "abc";
string s2 = "ab";
string s3 = s2 + "c";
Console.WriteLine(string.IsInterned(s3)); // abc
Console.WriteLine(String.ReferenceEquals(s1, s3)); // False
I just cannot understand why s3 interned, but ReferenceEquals was False.
Dose they have two copies in intern pool?
Thanks in advance.
There are basically three different situations possible when
string.IsInternedis invoked. To illustrate, here is a test method:You can "hit" all three situations with this code:
Output:
Since the literal
"0"is mentioned in the program text, a string instance with value"0"will exist in the intern pool. The variablexis a reference to that instance.The variable
yhas the same value asx, but that is not computed until run-time (the C# compiler makes no guesses as to whatint.ToString(IFormatProvider)could be returning). As a consequence,yis another instance than isx.The variable
zhas a value that is not found in the intern pool.