String.Intern有值吗?

Roy*_*mir 3 .net c# string-interning

String.Intern 有一个特殊的字符串池,以后可以检索.

有没有办法让我知道指定的字符串从池中获取的,并且是否是新创建的?例如:

string s1 = "MyTest"; 
string s2 = new StringBuilder().Append("My").Append("Test").ToString(); 
string s3 = String.Intern(s2); 
Console.WriteLine((Object)s2==(Object)s1); // Different references.
Console.WriteLine((Object)s3==(Object)s1); // The same reference.
Run Code Online (Sandbox Code Playgroud)

S3参考VAL 从池中取出

有什么办法让我知道吗?

Fem*_*ref 5

您可能对该IsInterned方法有一些好运,null如果字符串未被实现则返回,如果从池中获取,则返回引用.但是,此行为将取决于运行时,并且可能无法产生您期望的结果.