In C#, StringBuilder.AppendLine(text) adds the text and then a line break to the end of the builder. Called with no argument, AppendLine() adds only the line break, which gives you an empty line.
The line break is Environment.NewLine: \r\n (two characters) on Windows and \n (one character) on Linux and macOS. That makes AppendLine a good fit for text shown on screen or saved for the same computer. When a file format needs one fixed ending, append '\n' yourself with Append(). The outputs below show each line separately, because the hidden line-break characters are not visible.
An instance method of StringBuilder (using System.Text;), called as sb.AppendLine(...). The builder is a mutable buffer that changes in place, unlike an immutable string. It is not thread-safe. AppendLine() and AppendLine(string) exist in every .NET version. An overload that takes an interpolated string directly arrived in .NET 6.
Syntax
Call it with text to finish a line, or with nothing to add a blank line.
C#
StringBuilder sb = new StringBuilder();
sb.AppendLine("first"); // "first" + newline
sb.AppendLine(); // just a newline (blank line)
sb.Append("last"); // no newline at the end
Call
What it does
sb.AppendLine("text")
Adds text, then Environment.NewLine.
sb.AppendLine()
Adds only Environment.NewLine. Use it to end a line built with Append or to add a blank line.
sb.AppendLine(null)
Same as AppendLine(): no text, just the line break.
sb.Append('\n')
Not AppendLine: always one \n, on every operating system.
Because the line break is 2 characters on Windows and 1 elsewhere, Length after AppendLine depends on the operating system. Do not hard-code it; subtract Environment.NewLine.Length per line if you need the count of visible characters.
Library declarations
C#
public StringBuilder AppendLine();
public StringBuilder AppendLine(string? value);
Parameters
Parameter
Description
value
Optional. The text to add before the line break. null or no argument means only the line break is added.
Return Value
Return Value
Description
StringBuilder
The same builder, now ending with a line break.
C# Examples
Example 1 — Build Three Lines of Text
Write two lines with AppendLine, a last line with Append, and check that the text holds a line break.
C#
using System;
using System.Text;
class Program {
static void Main() {
StringBuilder sb = new StringBuilder();
sb.AppendLine("Line one");
sb.AppendLine("Line two");
sb.Append("Line three");
Console.WriteLine(sb.ToString());
Console.WriteLine(sb.ToString().Contains(Environment.NewLine));
}
}
Output
Line one
Line two
Line three
True
How It Works
Text plus break. Each AppendLine("...") writes the words and then Environment.NewLine, so the next text starts on a new line.
Last line without a break.Append("Line three") adds no line break, so the text ends right after three and WriteLine supplies the final one.
Printed as lines. The console turns each hidden break into a real new line, so three separate lines appear.
The break is really there.Contains(Environment.NewLine) is True on any operating system, because it checks for the same ending AppendLine used.
Example 2 — A Small Receipt With a Blank Line
Use AppendLine() with no argument for an empty line, finish a line built from pieces, and count only the visible characters.
C#
using System;
using System.Text;
class Program {
static void Main() {
StringBuilder sb = new StringBuilder();
sb.AppendLine("RECEIPT");
sb.AppendLine();
sb.AppendLine("Tea x2");
sb.Append("Total: ").Append(40).AppendLine();
Console.Write(sb.ToString());
int breaks = 4 * Environment.NewLine.Length;
Console.WriteLine(sb.Length - breaks);
}
}
Output
RECEIPT
Tea x2
Total: 40
22
How It Works
An empty line.AppendLine() right after the title adds a break with no text, which shows up as the blank line under RECEIPT.
Finish a pieced line.Append("Total: ").Append(40) builds the last line in parts, and the chained AppendLine() ends it.
Write, not WriteLine. The text already ends with a break, so Console.Write prints it without adding an extra empty line.
OS-neutral count. Four AppendLine calls added four breaks. Subtracting 4 * Environment.NewLine.Length leaves 7 + 0 + 6 + 9 = 22 visible characters on Windows and Linux alike.
Example 3 — AppendLine vs a Fixed \n
Show that AppendLine equals Append(Environment.NewLine), then build text with a fixed '\n' whose length never changes.
C#
using System;
using System.Text;
class Program {
static void Main() {
StringBuilder a = new StringBuilder();
a.AppendLine("x");
StringBuilder b = new StringBuilder();
b.Append("x").Append(Environment.NewLine);
Console.WriteLine(a.ToString() == b.ToString());
StringBuilder unix = new StringBuilder();
unix.Append("one").Append('\n').Append("two").Append('\n');
Console.WriteLine(unix.Length);
Console.Write(unix.ToString());
}
}
Output
True
8
one
two
How It Works
Two ways, one result.AppendLine("x") and Append("x").Append(Environment.NewLine) produce the same text, so the comparison prints True.
A fixed ending. The unix builder uses '\n', a single character, no matter which operating system runs the code.
Predictable length. 3 + 1 + 3 + 1 = 8, and that number is the same on Windows and Linux. With AppendLine it would be 10 on Windows.
Still two lines. Consoles on all systems treat \n as a new line, so one and two print on separate lines.
Frequently Asked Questions
It adds optional text and then a line break to the end of the builder. With no argument it adds only the line break. It returns the same builder.
Environment.NewLine. That is \r\n (two characters) on Windows and \n (one character) on Linux and macOS.
Use sb.Append(text).Append('\n') instead of AppendLine. Many file formats and network protocols expect one fixed line ending.
If the last line was added with AppendLine, the text already ends with a line break, and Console.WriteLine adds another. Use Console.Write or end with Append.
AppendLine takes a string. For a number, chain sb.Append(42).AppendLine(), or pass an interpolated string such as sb.AppendLine($"Total: {total}").