已经过的String.intern的价值?价值、String、intern

2023-09-05 01:12:26 作者:徒留一场笑谈一场心伤

的String.intern 具有稍后可检索的字符串,特殊池。

String.Intern has a special pool for strings which can later be retrieved.

有什么办法,我知道,指定字符串的是取自池,并没有新创建的? 例如:

Is there any way for me to know that the specified string was taken from the pool , and was NOt newly created ? example :

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.

S3裁判VAL 是从池中获取

有什么办法,我知道了吗?

is there any way for me to know it ?

推荐答案

您可能有一些运气的 IsInterned 方法,它返回如果字符串不拘留,并返回一个参考,如果它是从池中获取。但是,此行为将取决于运行,并可能不会产生你所期望的。

You might have some luck with the IsInterned method, it returns null if the string wasn't interned, and returns a reference if it was taken from the pool. However, this behaviour will depend on the runtime, and might not yield what you expect.

 
精彩推荐