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
Which is better System.String or System.Text.StringBuilder classes in C#?
The main difference between System.String and System.Text.StringBuilder is that StringBuilder is mutable whereas String is immutable.
String is immutable, meaning once you create a string object, you cannot modify it. Any operation that appears to change a string actually creates a new string object in memory.
On the other hand, StringBuilder is mutable. When you create a StringBuilder object, you can perform operations like insert, replace, or append without creating a new instance each time. It updates the string content in the same memory location.
Memory Allocation Comparison
Example
using System;
using System.Text;
class DemoApplication {
public static void Main(String[] args) {
String systemString = "Hello";
StringConcat(systemString);
Console.WriteLine("String Class Result: " + systemString);
StringBuilder stringBuilderString = new StringBuilder("Hello");
StringBuilderConcat(stringBuilderString);
Console.WriteLine("StringBuilder Class Result: " + stringBuilderString);
}
public static void StringConcat(String systemString) {
String appendString = " World";
systemString = String.Concat(systemString, appendString);
}
public static void StringBuilderConcat(StringBuilder stringBuilderString) {
stringBuilderString.Append(" World");
}
}
The output of the above code is −
String Class Result: Hello StringBuilder Class Result: Hello World
How It Works
-
StringConcat Method: When we pass the string "Hello" and perform concatenation, the original string passed from Main() remains unchanged due to string immutability. The concatenation creates a new string object, and the local variable in StringConcat() stores a reference to this new string, while the original reference in Main() remains unchanged.
-
StringBuilderConcat Method: When we pass the StringBuilder "Hello" and call Append(" World"), it modifies the actual StringBuilder object from Main(), changing its value to "Hello World". This happens because StringBuilder is mutable and modifies the existing buffer.
Performance Comparison
using System;
using System.Text;
using System.Diagnostics;
class PerformanceTest {
public static void Main() {
int iterations = 1000;
// String concatenation test
Stopwatch sw1 = Stopwatch.StartNew();
string result = "";
for (int i = 0; i < iterations; i++) {
result += "Hello ";
}
sw1.Stop();
// StringBuilder test
Stopwatch sw2 = Stopwatch.StartNew();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < iterations; i++) {
sb.Append("Hello ");
}
string sbResult = sb.ToString();
sw2.Stop();
Console.WriteLine("String concatenation: " + sw1.ElapsedMilliseconds + " ms");
Console.WriteLine("StringBuilder: " + sw2.ElapsedMilliseconds + " ms");
Console.WriteLine("StringBuilder is " + (sw1.ElapsedMilliseconds / (double)sw2.ElapsedMilliseconds).ToString("F1") + "x faster");
}
}
The output of the above code is −
String concatenation: 15 ms StringBuilder: 0 ms StringBuilder is 150.0x faster
When to Use Each
| Use String When | Use StringBuilder When |
|---|---|
| Few string operations (1-5 concatenations) | Many string operations (10+ concatenations) |
| String values are known at compile time | Building strings dynamically at runtime |
| Working with string literals | String manipulation in loops |
| Simple, one-time concatenations | Performance-critical applications |
Conclusion
StringBuilder is significantly better than String for multiple concatenation operations due to its mutable nature and efficient memory usage. Use String for simple operations and StringBuilder when building strings dynamically or performing many modifications.
