Faster for frequent append/insert/remove operations
Suitable for fixed or rarely changing text
Suitable for dynamically changing text
Defined in System.String
Defined in System.Text.StringBuilder
Example:
// String
string str = "Hello";
str += " World"; // Creates a new string object
// StringBuilder
StringBuilder sb = new StringBuilder("Hello");
sb.Append(" World"); // Modifies existing buffer
Interview answer (short):
string is immutable, meaning any modification creates a new object.
StringBuilder is mutable, allowing text to be modified without creating new objects repeatedly, making it more efficient for frequent string manipulations.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
String vs StringBuilder in C#
System.StringSystem.Text.StringBuilderExample:
Interview answer (short):