繁体   English   中英

为什么字符串实习但有不同的引用?

[英]Why string interned but has different references?

string s1 = "abc";
string s2 = "ab";
string s3 = s2 + "c";

Console.WriteLine(string.IsInterned(s3));           // abc
Console.WriteLine(String.ReferenceEquals(s1, s3));  // False

我只是不明白为什么s3实习,但是ReferenceEquals是假的。

他们在实习池中有两份副本吗?

提前致谢。

它们是单独的参考。 字符串"abc"是实体,因为它是文字字符串。

表达式s2 + "c"被编译为string.Concat(s2, "c") ..这导致一个新的(和单独的)字符串引用。

这是因为虽然该字符串的值确实被实现(因为文字“abc”),但它与实习字符串不是同一个实例,而是由+运算符生成的实例。

String.ReferenceEquals(s1, string.IsInterned(s3));

会回归真实

调用string.IsInterned时,基本上有三种不同的情况。 为了说明,这是一个测试方法:

static void MyInternCheck(string str)
{
  var test = string.IsInterned(str);

  if ((object)test == (object)str)
    Console.WriteLine("Yes, your string instance is in the intern pool");
  else if (test == str)
    Console.WriteLine("An instance with the same value exists in the intern pool, but you have a different instance with that value");
  else if (test == null)
    Console.WriteLine("No instance with that value exists in the intern pool");
  else
    throw new Exception("Unexpected intern pool answer");
}

您可以使用以下代码“命中”所有三种情况:

static void Main()
{
  string x = "0";
  MyInternCheck(x);
  string y = (0).ToString(CultureInfo.InvariantCulture);
  MyInternCheck(y);
  string z = (1).ToString(CultureInfo.InvariantCulture);
  MyInternCheck(z);
}

输出:

Yes, your string instance is in the intern pool
An instance with the same value exists in the intern pool, but you have a different instance with that value
No instance with that value exists in the intern pool

由于文字"0"在程序文本中提到,与值的字符串例如"0"将在实习生池存在。 变量x是对该实例的引用。

变量yx具有相同的 ,但是直到运行时才计算(C#编译器没有猜测int.ToString(IFormatProvider)可能返回的内容)。 因此, y是另一个实例,而不是x

变量z具有在实习池中找不到的值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM