Duplicate text is cheap to type and expensive to store if every copy is a distinct object. .NET’s answer is string interning: identical literals can share one instance in a process-wide intern pool.
Literals are interned
string s1 = "Welcome";
string s2 = "Welcome";
Console.WriteLine(object.ReferenceEquals(s1, s2)); // True
The compiler and runtime treat those literals as the same interned instance. ReferenceEquals is true not because strings use value equality here, but because there is only one object.
Runtime-built strings do not automatically join that pool:
string s1 = new string("Welcome".ToCharArray());
string s2 = "Welcome";
Console.WriteLine(object.ReferenceEquals(s1, s2)); // False
You can opt in:
string s1 = new string("Welcome".ToCharArray());
string interned = string.Intern(s1);
Console.WriteLine(object.ReferenceEquals(interned, "Welcome")); // True
string.IsInterned tells you whether a value is already in the pool without adding it.
When it actually matters
Interning pays off when the same token appears thousands of times and lives for the life of the process — think repeated status codes, country names, or configuration keys loaded from files. Equality can then become a reference check in some paths, and you avoid N copies of the same payload.
It is the wrong default for short-lived or unique strings. The intern pool is not a cache you get back. Interned instances stay rooted for the lifetime of the AppDomain / process. Interning every user-generated message is a way to grow working set with no benefit.
Practical rule
- Trust literals. The compiler already interned them.
- Intern high-cardinality-repeat, long-lived tokens you construct yourself — after you have measured duplication.
- Never intern unbounded input.
- Prefer
StringComparerand spans over clever intern tricks in hot parsing loops.
Interning is a hidden memory optimization, not a general-purpose deduplicator. Use it where the duplicates are real and the lifetime is known.
Expanded from a public note I first published on LinkedIn in March 2025.