加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – String.Intern有值吗?

发布时间:2020-12-16 00:02:03 所属栏目:百科 来源:网络整理
导读:String.Intern有一个特殊的字符串池,以后可以检索它. 有没有办法让我知道指定的字符串是从池中获取的,并且是否是新创建的? 例如: string s1 = "MyTest"; string s2 = new StringBuilder().Append("My").Append("Test").ToString(); string s3 = String.Int
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 ref val取自游泳池

有什么办法让我知道吗?

解决方法

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读