Why some identical strings are not interned in .NET?

后端 未结 5 1552
悲&欢浪女
悲&欢浪女 2020-12-21 00:17
string s1 = \"test\";
string s5 = s1.Substring(0, 3)+\"t\"; 
string s6 = s1.Substring(0,4)+\"\";   
Console.WriteLine(\"{0} \", object.ReferenceEquals(s1, s5)); //Fa         


        
5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-21 00:58

    Strings in .NET can be interned. It isn't said anywhere that 2 identical strings should be the same string instance. Typically, the compiler will intern identical string literals, but this isn't true for all strings, and is certainly not true of strings created dynamically at runtime.

提交回复
热议问题