Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is the difference between a mutable and immutable string in C#?
In C#, mutable strings can be modified after creation, while immutable strings cannot be changed once created. The StringBuilder class represents mutable strings, whereas the string class represents immutable strings.
When you modify an immutable string, .NET creates a new string object in memory. With mutable strings using StringBuilder, modifications are made to the existing object without creating new memory allocations.
Immutable String
A string in C# is immutable, meaning once created, it cannot be modified. Any operation that appears to modify a string actually creates a new string object in memory.
Syntax
string variableName = "value";
Example
using System;
class Program {
public static void Main() {
string str1 = "Hello";
string str2 = "World";
// This creates a new string object
string result = str1 + " " + str2;
Console.WriteLine("Original str1: " + str1);
Console.WriteLine("Combined result: " + result);
// String comparison
if (String.Compare(str1, "Hello") == 0) {
Console.WriteLine("str1 equals 'Hello'");
}
}
}
The output of the above code is −
Original str1: Hello Combined result: Hello World str1 equals 'Hello'
Mutable String
StringBuilder is a mutable string class in C#. You can modify the contents without creating new objects in memory, making it more efficient for multiple string operations.
Syntax
StringBuilder variableName = new StringBuilder();
StringBuilder variableName = new StringBuilder("initial value");
Example
using System;
using System.Text;
class Program {
public static void Main() {
StringBuilder str = new StringBuilder("Web World!!", 30);
// Modify the existing StringBuilder object
str.Replace("World", "Arena");
str.Append(" Welcome!");
Console.WriteLine("Modified StringBuilder: " + str);
Console.WriteLine("Capacity: " + str.Capacity);
Console.WriteLine("Length: " + str.Length);
}
}
The output of the above code is −
Modified StringBuilder: Web Arena!! Welcome! Capacity: 30 Length: 21
Performance Comparison
Example
using System;
using System.Text;
class Program {
public static void Main() {
// Inefficient with immutable string
string immutableStr = "";
for (int i = 0; i < 5; i++) {
immutableStr += "Step " + i + " ";
}
Console.WriteLine("Immutable result: " + immutableStr);
// Efficient with mutable StringBuilder
StringBuilder mutableStr = new StringBuilder();
for (int i = 0; i < 5; i++) {
mutableStr.Append("Step ").Append(i).Append(" ");
}
Console.WriteLine("Mutable result: " + mutableStr.ToString());
}
}
The output of the above code is −
Immutable result: Step 0 Step 1 Step 2 Step 3 Step 4 Mutable result: Step 0 Step 1 Step 2 Step 3 Step 4
Comparison
| Immutable String (string) | Mutable String (StringBuilder) |
|---|---|
| Cannot be modified after creation | Can be modified after creation |
| Creates new object for each modification | Modifies existing object in memory |
| Thread-safe by nature | Not thread-safe |
| Best for few string operations | Best for multiple string operations |
| Higher memory usage for concatenations | Lower memory usage for concatenations |
Conclusion
Use immutable string for simple operations and when thread safety is important. Use mutable StringBuilder when performing multiple string modifications to improve performance and reduce memory allocation overhead.
