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

180 Views Asked by At

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?

1

There are 1 best solutions below

1
Richard On BEST ANSWER

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