Has String.Intern had the value?

我是研究僧i 提交于 2019-12-23 20:05:34

问题


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 ref val was taken from the pool

is there any way for me to know it ?


回答1:


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.




回答2:


I think that IsInterned method could help you: http://msdn.microsoft.com/en-us/library/system.string.isinterned.aspx




回答3:


Yes, use String.IsInterned().

See the MSDN page.



来源:https://stackoverflow.com/questions/8688300/has-string-intern-had-the-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!