简体   繁体   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

I just cannot understand why s3 interned, but ReferenceEquals was False. 我只是不明白为什么s3实习,但是ReferenceEquals是假的。

Dose they have two copies in intern pool? 他们在实习池中有两份副本吗?

Thanks in advance. 提前致谢。

They are separate references. 它们是单独的参考。 The string "abc" is interned because it is a literal string. 字符串"abc"是实体,因为它是文字字符串。

The expression s2 + "c" is compiled to string.Concat(s2, "c") .. which results in a new (and separate) string reference. 表达式s2 + "c"被编译为string.Concat(s2, "c") ..这导致一个新的(和单独的)字符串引用。

This is because while the value of that string is indeed interned (because of the literal "abc"), it is not the same instance as the interned string, rather an instance generated by the + operator. 这是因为虽然该字符串的值确实被实现(因为文字“abc”),但它与实习字符串不是同一个实例,而是由+运算符生成的实例。

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

would return true 会回归真实

There are basically three different situations possible when string.IsInterned is invoked. 调用string.IsInterned时,基本上有三种不同的情况。 To illustrate, here is a test method: 为了说明,这是一个测试方法:

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");
}

You can "hit" all three situations with this code: 您可以使用以下代码“命中”所有三种情况:

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

Output: 输出:

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

Since the literal "0" is mentioned in the program text, a string instance with value "0" will exist in the intern pool. 由于文字"0"在程序文本中提到,与值的字符串例如"0"将在实习生池存在。 The variable x is a reference to that instance. 变量x是对该实例的引用。

The variable y has the same value as x , but that is not computed until run-time (the C# compiler makes no guesses as to what int.ToString(IFormatProvider) could be returning). 变量yx具有相同的 ,但是直到运行时才计算(C#编译器没有猜测int.ToString(IFormatProvider)可能返回的内容)。 As a consequence, y is another instance than is x . 因此, y是另一个实例,而不是x

The variable z has a value that is not found in the intern pool. 变量z具有在实习池中找不到的值。

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

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