String interning?

前端 未结 6 1440
抹茶落季
抹茶落季 2020-12-09 13:15

The second ReferenceEquals call returns false. Why isn\'t the string in s4 interned? (I don\'t care about the advantages of StringBuilder over string concatenation.)

6条回答
  •  抹茶落季
    2020-12-09 13:49

    First of all, everything written so far about immutable strings is correct. But there are some important things which are not written. The code

    string s1 = "tom";
    string s2 = "tom";
    Console.Write(object.ReferenceEquals(s2, s1)); //true
    

    display really "True", but only because of some small compiler optimization or like here because CLR ignore C# compiler attributes (see "CLR via C#" book) and place only one string "tom" in the heap.

    Second you can fix the situation with following lines:

    s3 = String.Intern(s3);
    s4 = String.Intern(s4);
    Console.Write (object.ReferenceEquals (s3, s4)); //true
    

    Function String.Intern calculates a hash code of the string and search for the same hash in the internal hash table. Because it find this, it returns back the reference to already existing String object. If the string doesn't exist in the internal hash table, a copy of the string is made and the hash computed. The garbage collector doesn't free memory for the string, because it is referenced by the hash table.

提交回复
热议问题