Why is string interning failing here (or is it)?

Viewed 83

Consider the following C# code:

  var a = "123";
  var b = 123.ToString();
  Console.WriteLine(a==b);
  Console.WriteLine(ReferenceEquals(a, b));
  Console.WriteLine("a interned? " + string.IsInterned(a));
  Console.WriteLine("b interned? " + string.IsInterned(b));
  Console.WriteLine(
    ReferenceEquals(
      string.IsInterned(a),
      string.IsInterned(b)
      )
    );

This outputs

True
False
a interned? 123
b interned? 123
True

Everything about this makes sense except why ReferenceEquals(a,b) returns false considering a got interned and then b would refer to a, as indicated by the string.IsInterned checks.

0 Answers
Related