Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Strings and Text

String interning

Reference for C# string interning: how the intern pool works, which strings are interned automatically, string.Intern, IsInterned and ReferenceEquals.

The runtime keeps a table of unique strings called the intern pool. All string literals in your code go into it, so two identical literals are the same object in memory. string.Intern(s) looks up the pool: if an equal string is already there it returns that instance, otherwise it adds s and returns it.

Interning saves memory when you have many repeated long-lived strings, and it makes equality checks by reference possible. For almost all code you should still compare with == or Equals, which compare content and are correct either way.

C#
string a = "hello";
string b = "hel" + "lo";
Console.WriteLine(ReferenceEquals(a, b)); // True

Try it

C# Literals, runtime strings and Intern

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# IsInterned does not add to the pool

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
public static string Intern(string str)
public static string? IsInterned(string str)
Name Description
str The string to look up (and, for Intern, to add).

Returns: Intern returns the pooled instance, which is str itself if it was just added. IsInterned returns the pooled instance if there is an equal string in the pool, or null; it never adds anything.

Errors / undefined behavior: ArgumentNullException when str is null.

Usage notes

  • Automatic interning. Literals and compile-time constant expressions ("a" + "b", const strings) are interned. Strings produced at run time (concatenation of variables, Substring, ToString(), ReadLine, deserialization) are not.
  • Use ==, not ReferenceEquals. Content comparison is always correct. Relying on reference equality is a bug waiting to happen as soon as one side comes from user input.
  • Interned strings are not freed while the runtime runs. The pool is not a normal cache, so interning large amounts of unique data (user input, ids from a feed) leaks memory. Intern only a small, bounded set of repeated values.
  • Interning costs a lookup. Intern hashes the string and takes a lock, so it is much slower than a dictionary lookup in a thread-local cache. Application-specific caches (a Dictionary<string,string> you can clear) are often a better design.
  • Switch statements and dictionaries do not need it. They hash the content, so interning does not make them faster.
  • Behavior across versions. string.Empty and "" are the same object on current .NET, but treat identity of any string as an implementation detail unless you created the string through Intern yourself.
  • Reference-equality tricks are sometimes used as a fast path: if (ReferenceEquals(a, b)) return true; before a content comparison. That is safe, because equal references are always equal strings.