Tim Schmelter
Tim Schmelter

Reputation: 460058

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?

Upvotes: 1

Views: 206

Answers (1)

Richard
Richard

Reputation: 108975

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

Upvotes: 3

Related Questions