LearnThatStack Ace your next interview
C# · question
Question 10 of 112

Explain the difference between string and StringBuilder.

beginner
← All C# questions
Re-explain
Visual ↓

A string is immutable, so every change creates a new string object. A StringBuilder is mutable and appends into an internal buffer, so repeated changes avoid those extra objects.

Concatenating in a loop shows the cost. Each += on a string copies all the text built so far, so the total work grows with the square of the length. StringBuilder keeps one buffer and only reallocates when its capacity runs out.

Use string for a few concatenations, where the difference is too small to matter. Use StringBuilder when you build text in a loop or across many steps.

Immutability also makes a string safe to share across threads. A StringBuilder is not thread-safe, so one thread should own it.

// Inefficient - creates multiple string objects
string result = "";
for (int i = 0; i < 1000; i++)
{
    result += i.ToString(); // Creates new string each time
}
// Efficient - uses internal buffer
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 1000; i++)
{
    sb.Append(i); // Modifies existing buffer
}
string result2 = sb.ToString();
Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
See it - interactive diagram
string += vs StringBuilder.Append in a loop
after i = 1
Stack
result2
sb1
Heap
1StringBuilder
buffer"01"
Length2
Capacity16
2String
text"01"
chars copied2

Both hold 01 after two passes. sb has room for 16 characters and grows only when that runs out.

after i = 2result += i; sb.Append(i);
Stack
resultchanged3
sb1
Heap
1StringBuilder
bufferchanged"012"
Lengthchanged3
Capacity16
3Stringnew
text"012"
chars copied3
2Garbage string
text"01"
chars copied2

+= copied all 3 characters into a new string. Nothing points at 01 now, so it is garbage.

after i = 3result += i; sb.Append(i);
Stack
resultchanged4
sb1
Heap
1StringBuilder
bufferchanged"0123"
Lengthchanged4
Capacity16
4Stringnew
text"0123"
chars copied4
2Garbage string
text"01"
chars copied2
3Garbage string
text"012"
chars copied3

Now += copied 4 characters, and that number keeps growing. sb is still one object with room for 12 more.

Every += copies all the text so far into a new string and leaves the old one as garbage, but StringBuilder adds to one buffer and only grows it when full.

© LearnThatStack - diagrams may not be republished without permission.

Other questions have diagrams like this too. Sign in to ask for one where it's missing - it's free.

Create a free account
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

  • Is `a + b + c` in one expression as costly as += in a loop? Why or why not? The compiler turns a single expression into one String.Concat call, not one new string per +. The cost comes from repeating += across a loop.
  • When would you give a StringBuilder an initial capacity, and what does that save? If you can estimate the final length, the builder starts with enough room and skips the growth steps it would need as text arrives.
  • Two threads need to add to the same text. How would you handle it? StringBuilder is not thread-safe. Either lock around every append, or let each thread build its own text and combine the results at the end.
  • How would you join a list of 500 names with commas? Would you reach for StringBuilder? string.Join takes a collection and a separator in one call. Use StringBuilder when the text is built step by step with logic in between.
  • Why can the runtime safely share one copy of identical string literals, and what is that called? Name interning and tie it to immutability. Code holding the shared copy can't change it under anyone else.

What you can say

  1. A string in C# is immutable, so every change gives you a new string object, while a StringBuilder is mutable and appends into one internal buffer.
  2. The difference shows up when you concatenate in a loop.
  3. Each += on a string copies all the text built so far, so the total work grows with the square of the length.
  4. A StringBuilder keeps one buffer and only reallocates when its capacity runs out, and you call ToString once at the end.
  5. So I use a plain string for a few concatenations, where the difference is too small to matter, and StringBuilder when I build text in a loop or over many steps.
  6. Immutability also makes a string safe to share across threads, but a StringBuilder is not thread-safe, so one thread should own it.

Weak answers to avoid

  • Says StringBuilder is always faster For two or three joins the gain is too small to matter and a string reads better. Say when to switch, which is text built in a loop or over many steps.
  • Thinks += changes the string in place That misreads immutability. Each += makes a new string and points the variable at it. The old one is left for the garbage collector.
  • Says 'immutable' but never explains the cost The interviewer wants the mechanism. Say that each += copies all the text so far, so a loop does work that grows with the square of the length.
  • Shares one StringBuilder across threads StringBuilder is not thread-safe, so concurrent appends can corrupt the result. Give each builder one owning thread, or lock around it.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the C# cheatsheet.

← Back to all C# questions
Pro · $10/mo

98 of 112 C# answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime