StringBuilder and literal syntax
StringBuilder is a mutable text buffer for constructing a string in stages. Append pieces to the buffer and call ToString when you need the completed immutable string. Ordinary interpolation remains convenient for a short message.
using System;using System.Text;var report = new StringBuilder();for (int box = 1; box <= 3; box++){ if (box > 1) report.Append(", "); report.Append($"Box {box}");}Console.WriteLine(report.ToString());The separator is added before every box except the first, so no trailing comma appears.
Expected output (reasoned, not executed):
Box 1, Box 2, Box 3Append in a loop
Create a System.Text.StringBuilder with new. Append adds content to the same builder, and ToString() produces a string snapshot. Multiple variables may refer to the same builder. Repeated concatenation can create intermediate strings; use a builder when constructing variable amounts of text, and measure performance if it matters.
Choose a literal style
Regular literals interpret escape sequences such as \n and \". Verbatim literals begin with @ and keep backslashes literal; double a quote inside them. Raw literals start and end with at least three double quotes and are available from C# 11. In a multiline raw literal, indentation is determined by the closing delimiter; choose delimiters longer than any run of quotes inside.
Control line endings intentionally
AppendLine() uses the platform newline, while Append("\n") adds a specific LF. The latter is useful when a protocol explicitly requires LF. Raw strings preserve content line breaks, so tests comparing exact bytes should fix the source line-ending convention. A raw string is not JSON validation or escaping of runtime data.
using System;string regular = "C:\\lab\\notes.txt";string verbatim = @"C:\lab\notes.txt";string raw = """C:\lab\notes.txt""";Console.WriteLine(regular == verbatim && verbatim == raw);All three forms represent the same characters. No filesystem operation occurs.
Expected output (reasoned, not executed):
Trueusing System;using System.Text;var buffer = new StringBuilder("Draft");string snapshot = buffer.ToString();buffer.Append(" complete");Console.WriteLine(snapshot);Console.WriteLine(buffer.ToString());The snapshot is an immutable string; subsequent buffer mutation does not change it.
Expected output (reasoned, not executed):
DraftDraft completeCommon mistake and correction
Do not assume StringBuilder is faster for every expression or share a mutable builder across threads without coordination. For a fixed two-value label, interpolation is usually clearer. Use a JSON serializer instead of assembling untrusted values into JSON using raw literals.
Try it yourself
Build Item 1 | Item 2 | Item 3 | Item 4 with a loop and a builder. Change the count to zero and verify that the result is an empty string.
Next: Dates, times and durations. Examples target C# 14 / net10.0. They are static study material; compiler and ByteMind execution have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
StringBuilder and literal syntax — knowledge check
Apply the lesson concepts to the following scenarios.
What happens to a builder after Append?
What does ToString return?
Which literal form treats backslashes literally?
Which C# release introduced raw string literals?
Why put the separator before every item except the first?