C# String.Intern有这个值吗?
C# String.Intern有这个值吗?,c#,.net,string-interning,C#,.net,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
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.
s3参考值取自池中
有什么办法让我知道吗?我认为IsInterned方法可以帮助您:您可能会很幸运地使用
IsInterned
方法,如果字符串未被插入,它将返回null
,如果它是从池中获取的,它将返回一个引用。但是,此行为将取决于运行时,可能不会产生您期望的结果。是,请使用String.IsInterned()
看