Search code examples
c#.netstringstring-interning

Why a string is interned that is created via constructor and with a char[]?


I was sure that this would output false, but actually it outputs true:

string foo = new string("foo".ToCharArray());
Console.WriteLine(string.IsInterned(foo) != null); // true

I thought that creating it already via constructor would prevent string interning. But even using a char[] causes it to be interned. What is the reason or what is my error in reasoning?


Solution

  • Literal strings are interned. And you have the literal foo in your source code.