C# String Intern() Method

Intermediate
5 min read
Updated: Nov 2026
3 examples
String

Definition

.NET keeps a hidden table called the intern pool: one shared copy of each string placed there. Every string literal in your code, like "hello", is put in the pool automatically, so all copies of the same literal are actually one object.

string.Intern(s) looks up the text of s in the pool. If the same text is already there, it returns that pooled object. If not, it adds s to the pool and returns it. Either way you get the one shared copy. The text is the same as before; only which object you hold can change.

This is a static member of System.String. Call it on the type, such as string.Intern(...), not on a string variable. Strings are immutable, which is exactly why sharing one pooled copy is safe: nobody can change it. Intern never edits the string you pass; it returns a reference.

Syntax

Pass the string; keep the returned reference.

C#
string built = new string(new[] { 'h', 'i' });
string pooled = string.Intern(built);

bool sameObject = ReferenceEquals(pooled, "hi");   // True: the literal "hi" is in the pool
CallWhat it does
string.Intern(s)Return the pooled copy of s (adding it if missing).
s = string.Intern(s)Replace your reference with the shared one.
ReferenceEquals(a, b)Check whether two variables point to the same object (not just equal text).

Interned strings normally stay in memory until the process ends, so interning many unique values (like user input) wastes memory. Interning pays off only for a small set of values repeated very many times. For ordinary equality, == already compares text, so you do not need interning to compare strings. Results here describe the standard .NET runtime; ahead-of-time compiled apps may pool literals differently.

Library declarations
C#
public static string Intern(string str);

Parameters

ParameterDescription
strRequired. The string to look up or add. null throws ArgumentNullException.

Return Value

Return ValueDescription
stringThe pooled string with the same text as str. It may be a different object from str.

C# Examples

Example 1 — Get the Literal's Object Back

A string built at run time has the same text as a literal but is a different object, until you intern it.

C#
using System;

class Program {
    static void Main() {
        string literal = "hello";
        string built = new string(new[] { 'h', 'e', 'l', 'l', 'o' });

        Console.WriteLine(literal == built);
        Console.WriteLine(ReferenceEquals(literal, built));

        string pooled = string.Intern(built);
        Console.WriteLine(ReferenceEquals(literal, pooled));
    }
}

How It Works

  1. Equal text. == compares characters, and both spell hello, so it prints True.
  2. Two objects. new string(...) always creates a fresh object, so ReferenceEquals is False.
  3. The pool already knows hello. The literal was placed in the pool, so Intern(built) returns that literal's object.
  4. Now they are one. pooled and literal point to the same object. built itself is unchanged and is simply no longer needed.

Example 2 — Share One Copy of Strings Built at Run Time

Build the same text twice at run time, then intern both and compare identities.

C#
using System;

class Program {
    static void Main() {
        string part = "c";
        string a = "ab" + part;
        string b = "ab" + part;

        Console.WriteLine(a == b);
        Console.WriteLine(ReferenceEquals(a, b));

        string ia = string.Intern(a);
        string ib = string.Intern(b);
        Console.WriteLine(ReferenceEquals(ia, ib));
    }
}

How It Works

  1. Built while running. part is a variable, so "ab" + part is joined at run time and each line makes a new string.
  2. Same text, different objects. a == b is true, but they are two separate copies in memory.
  3. First intern adds, second finds. Intern(a) stores the text in the pool; Intern(b) finds it there and returns the same object.
  4. Where it saves memory. If thousands of objects held these strings, keeping only the interned references would let the duplicates be collected.

Example 3 — Constants Are Joined and Pooled by the Compiler

Joining two literals happens at compile time, so the result is already pooled. A run-time join is not, until you intern it.

C#
using System;

class Program {
    static void Main() {
        string x = "C#";
        string y = "C" + "#";
        string z = new string('C', 1) + "#";

        Console.WriteLine(ReferenceEquals(x, y));
        Console.WriteLine(ReferenceEquals(x, z));
        Console.WriteLine(ReferenceEquals(x, string.Intern(z)));
    }
}

How It Works

  1. Folded by the compiler. "C" + "#" contains only constants, so the compiler turns it into the single literal "C#".
  2. One pooled literal. Because x and y are the same literal, they are the same object: True.
  3. A run-time join. new string('C', 1) is created while the program runs, so z is a new object with equal text: False.
  4. Intern finds the literal. string.Intern(z) returns the pooled "C#", which is x.

Frequently Asked Questions

It returns the single shared copy of a string from the runtime intern pool. If the text is not in the pool yet, the string you passed is added and returned.
Yes. The runtime puts string literals in the intern pool, so the same literal used in many places is one object.
Only if you compare references with ReferenceEquals, which is risky. Normal == compares text and works without interning. Interning is mainly a memory optimization.
Yes. Interned strings usually live until the process ends. Interning many different values, such as user input, can increase memory use.
Intern adds the string to the pool if it is missing and always returns a string. IsInterned only looks: it returns the pooled string, or null if the text is not in the pool.

Next: IsInterned()

Look up a string in the intern pool without adding it; returns the pooled copy or null.

IsInterned() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful