C# Strings Aren’t Really Immutable (Here’s Proof)

C# strings are immutable… until they aren’t.

In this video, we dig into .NET string interning—why identical string literals can point to the same instance in memory—and how to prove it with Object.ReferenceEquals(). Then we look at the oddball API String.IsInterned(), why it returns a string (or null) instead of a bool, and how String.Intern() forces a runtime-created string into the intern pool.

Finally, for educational purposes only, we do the “cursed” experiment: using AsSpan(), MemoryMarshal.GetReference(), and Unsafe.Add() to take a writable reference to the underlying UTF-16 buffer and mutate a string literal. This demonstrates how breaking string immutability can violate runtime assumptions and cause extremely subtle bugs—especially because interned literals may be shared across your entire program.

Warning: The unsafe mutation technique shown here is not supported and can corrupt hash tables, break security assumptions, and crash your process. Do not use it in production.

Topics covered
• What string interning is (and why it exists)
• Proving interning with ReferenceEquals
• String.IsInterned() and String.Intern()
• Low-level string layout (object header, length, UTF-16 chars)
• Unsafe mutation via byrefs and pointer arithmetic
• Why mutating interned strings is particularly dangerous

Subscribe for more low-level .NET tips.