C# String IsInterned() Method

Intermediate
5 min read
Updated: Nov 2026
3 examples
String

Definition

In C#, string.IsInterned() asks the runtime's intern pool whether it already holds a string with the same text. If it does, you get that pooled string back. If it does not, you get null. Unlike Intern(), it never adds anything.

Despite the name, it does not return a bool. Test the result with != null. All string literals in your program are in the pool, so looking up the text of a literal you have used finds it.

This is a static member of System.String. Call it on the type, such as string.IsInterned(...), not on a string variable. It only looks; it never adds anything to the pool and never changes the string you pass.

Syntax

Pass a string, then check the result for null.

C#
string? pooled = string.IsInterned(text);

if (pooled != null) {
    // the same text is in the pool; pooled is the shared copy
}
CallWhat it does
string.IsInterned(s)The pooled string, or null.
string.IsInterned(s) != nullA yes or no check.
string.IsInterned(s) ?? sUse the shared copy when there is one, otherwise keep s.

Passing null throws ArgumentNullException. Whether a run-time string is in the pool can depend on what other code has interned, so treat a null result as “not pooled right now”, not as a fact about the text. Results here describe the standard .NET runtime.

Library declarations
C#
public static string? IsInterned(string str);

Parameters

ParameterDescription
strRequired. The text to look up. Must not be null.

Return Value

Return ValueDescription
stringThe pooled string with the same text as str.
nullNo string with that text is in the pool.

C# Examples

Example 1 — Look Up a Literal and a Brand-New String

A literal is in the pool; a string with unique text built at run time is not.

C#
using System;

class Program {
    static void Main() {
        string literal = "hello";
        string built = new string('q', 7) + "zx9";

        Console.WriteLine(string.IsInterned(literal) != null);
        Console.WriteLine(ReferenceEquals(string.IsInterned(literal), literal));
        Console.WriteLine(string.IsInterned(built) == null);
    }
}

How It Works

  1. Literals are pooled. "hello" appears in the source code, so the lookup finds it and the result is not null.
  2. The same object comes back. For a literal, the pooled copy is the literal itself, so ReferenceEquals prints True.
  3. Unique run-time text. qqqqqqqzx9 is created while running and never written as a literal, so it is not in the pool.
  4. No side effect. The lookup did not add built; calling IsInterned(built) again would still return null.

Example 2 — Before and After Calling Intern

Look up a run-time key, intern it, and look it up again.

C#
using System;

class Program {
    static void Main() {
        string key = "order-" + 77.ToString();

        Console.WriteLine(string.IsInterned(key) == null);

        string.Intern(key);

        Console.WriteLine(string.IsInterned(key) != null);
        Console.WriteLine(ReferenceEquals(string.IsInterned(key), key));
    }
}

How It Works

  1. A key made at run time. 77.ToString() produces "77", and the join creates a new string order-77 that no literal spells out.
  2. Not there yet. The first lookup returns null, so == null prints True.
  3. Intern adds it. Because the text was missing, string.Intern(key) stores key itself in the pool.
  4. Found, and it is our object. The second lookup succeeds and returns the very same object as key.

Example 3 — Check Input Against Known Literal Words

Build two words at run time and see which one matches text already pooled by a literal.

C#
using System;

class Program {
    static void Main() {
        string[] known = { "start", "stop" };

        string first = new string(new[] { 's', 't', 'o', 'p' });
        string second = new string(new[] { 'j', 'u', 'm', 'p' });

        foreach (string word in new[] { first, second }) {
            string? pooled = string.IsInterned(word);
            if (pooled != null) {
                Console.WriteLine(word + " is in the pool: " + ReferenceEquals(pooled, known[1]));
            } else {
                Console.WriteLine(word + " is not in the pool");
            }
        }
    }
}

How It Works

  1. Literals load first. Creating the known array runs the literals "start" and "stop", so both are pooled before any lookup.
  2. Built letter by letter. first and second are fresh objects, not literals.
  3. A match returns the literal. The text stop is found, and the pooled object is the one stored in known[1].
  4. No match, no entry. No literal spells jump, so the lookup returns null and nothing is added.

Frequently Asked Questions

It returns the pooled string with the same text if one exists, or null if the text is not in the intern pool. It does not return a bool.
No. It only looks. Use string.Intern to add a string to the pool.
Yes, on the standard .NET runtime, once the code that uses the literal has been loaded. Literals are placed in the intern pool automatically.
Compare the result with null: string.IsInterned(s) != null.
Rarely: mainly in memory tuning, to reuse a pooled copy when one exists without growing the pool. Everyday code compares strings with == and never needs it.

Next: IsNormalized()

Check whether a string is already in a Unicode normalization form, without creating a copy.

IsNormalized() 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.

11 people found this page helpful