C# Programming › Strings in C#

Are string objects mutable or immutable in C#?

Beginner

Answer

String objects in C# are immutable, meaning that once a string is created, its actual content in memory can never be changed. Any operation that appears to modify a string, such as concatenation or replacing characters, actually creates a brand new string object in memory and leaves the original string untouched, which is an important detail to understand when reasoning about performance and memory usage in string heavy code.
string greeting = "Hello";
greeting = greeting + " World";
// This creates a new string object, the original "Hello" is unchanged in memory
Real-world example A developer investigating a memory heavy application discovers that thousands of string concatenations inside a loop were each creating a brand new string object, and switches to StringBuilder to avoid this repeated allocation.

Common follow-ups: Why does C# make strings immutable instead of mutable?;What is the performance impact of repeated string concatenation?