The second ReferenceEquals call returns false. Why isn't the string in s4 interned?
string s1 = "tom";
string s2 = "tom";
Console.Write(object.ReferenceEquals(s2, s1)); //true
string s3 = "tom";
string s4 = "to";
s4 += "m";
Console.Write(object.ReferenceEquals(s3, s4)); //false
--edit--
To add to the question, this discussion is about string interning and references. Not about the advantages of StringBuilder over string concatenation. Thanks.
--edit2-- Just noticed that when I do String.Intern(s4);, I still get false.
--edit3-- Getting weird now. Both s3 and s4 are interned but their references are not equal? Help!
string s3 = "tom";
string s4 = "to";
s4 += "m";
String.Intern(s4);
Console.WriteLine(s3 == s4); //true
Console.WriteLine(object.ReferenceEquals(s3, s4)); //false
Console.WriteLine(string.IsInterned(s3) != null); //true (s3 is interned)
Console.WriteLine(string.IsInterned(s4) != null); //true (s4 is interned)