In C#, StringBuilder.Append(value) adds a value to the end of the text in a builder. The value can be a string, a char, a number, a bool, a char[], or any object; non-text values are turned into text first, just like ToString() would.
With a normal string, every + copies all the text into a new string, because strings cannot change. Append just writes after the last character. It returns the same builder, so calls chain: sb.Append("a").Append(1). For a line break too, use AppendLine().
An instance method of StringBuilder (using System.Text;), called as sb.Append(...). The builder is a mutable buffer that changes in place, unlike an immutable string. It is not thread-safe. Append(string), Append(char), and the number overloads exist in every .NET version. Append(StringBuilder) and the span overloads were added in .NET Core 2.1 and later.
Syntax
Pass the value to add. Extra overloads take a repeat count or a slice of a string or array.
C#
StringBuilder sb = new StringBuilder();
sb.Append("Hi"); // Hi
sb.Append(' ').Append(7); // Hi 7
sb.Append('-', 3); // Hi 7---
sb.Append("abcdef", 1, 2); // Hi 7---bc
Call
What it does
sb.Append("text")
Adds the whole string. A null string adds nothing.
sb.Append(42)
Adds the number as text, here 42. Booleans become True or False.
sb.Append('*', 5)
Adds the same character 5 times: *****.
sb.Append("abcdef", 1, 3)
Adds part of a string: 3 characters starting at index 1, here bcd.
Number overloads such as Append(double) use the current culture, so Append(1.5) may print 1,5 on some machines. For fixed output, append value.ToString(CultureInfo.InvariantCulture). A bad slice (negative, or past the end) throws ArgumentOutOfRangeException.
Library declarations
C#
public StringBuilder Append(string? value);
public StringBuilder Append(string? value, int startIndex, int count);
public StringBuilder Append(char value);
public StringBuilder Append(char value, int repeatCount);
public StringBuilder Append(char[]? value, int startIndex, int charCount);
public StringBuilder Append(int value); // also bool, long, double, object, ...
public StringBuilder Append(ReadOnlySpan<char> value); // .NET Core 2.1+
Parameters
Parameter
Description
value
The value to add. Strings and chars are added as they are; other types are converted to text. null adds nothing.
repeatCount
Optional, with a char. How many times to add the character. Must be 0 or more.
startIndex, count
Optional, with a string or char[]. Where the slice starts and how many characters to take.
Return Value
Return Value
Description
StringBuilder
The same builder you called it on (not a copy), now with the value added at the end. That is what makes chaining work.
C# Examples
Example 1 — Build a Sentence Piece by Piece
Add a word, a comma, a space, another word, and a !, then print the text and its length.
C#
using System;
using System.Text;
class Program {
static void Main() {
StringBuilder sb = new StringBuilder();
sb.Append("Hello");
sb.Append(',');
sb.Append(' ');
sb.Append("World");
sb.Append('!');
Console.WriteLine(sb.ToString());
Console.WriteLine(sb.Length);
}
}
Output
Hello, World!
13
How It Works
Five small additions. Strings and single char values both go through Append; each one lands right after the previous text.
Nothing is copied around. All five calls write into the one buffer created by new StringBuilder(). With +, each step would have produced a new string.
Read the result.ToString() gives Hello, World!.
Count the characters.Length is 5 + 1 + 1 + 5 + 1 = 13, one for each character added.
Example 2 — Chain Calls With Numbers and Booleans
Chain several Append calls in one statement, mixing text, an int, and a bool. Then prove that Append returns the same object.
C#
using System;
using System.Text;
class Program {
static void Main() {
StringBuilder sb = new StringBuilder();
sb.Append("Items: ").Append(3).Append(", paid: ").Append(true);
Console.WriteLine(sb.ToString());
StringBuilder same = sb.Append('.');
Console.WriteLine(ReferenceEquals(same, sb));
Console.WriteLine(sb.ToString());
}
}
Output
Items: 3, paid: True
True
Items: 3, paid: True.
How It Works
One statement, four appends. Each Append returns the builder, so the next .Append is called on it straight away.
Values become text. The int 3 is written as 3 and the booltrue is written as True, with a capital T, the way .NET prints booleans.
Same object back.ReferenceEquals(same, sb) is True: the return value is not a copy, it is the very builder you called.
The dot is there. Because same and sb are one object, the . added through same shows up when you print sb.
Example 3 — Append Part of a String, Repeat a Char, and Skip null
Use the overloads that take a slice and a repeat count, and see that appending a null string is harmless.
C#
using System;
using System.Text;
class Program {
static void Main() {
string word = "programming";
StringBuilder sb = new StringBuilder();
sb.Append(word, 0, 7);
sb.Append(' ');
sb.Append('=', 3);
sb.Append(' ');
string missing = null;
sb.Append(missing);
sb.Append("done");
Console.WriteLine(sb.ToString());
Console.WriteLine(sb.Length);
}
}
Output
program === done
16
How It Works
Take a slice.Append(word, 0, 7) copies 7 characters starting at index 0 of "programming", which is program. No Substring call is needed.
Repeat one character.Append('=', 3) writes === in a single call, which is handy for simple dividers.
null is ignored. Appending a null string adds no characters and throws no error, so the text goes straight on to done.
Check the length. 7 + 1 + 3 + 1 + 4 = 16 characters, which matches program === done.
Frequently Asked Questions
It adds a value to the end of the text in the builder and returns the same builder. Strings and chars are added as they are; numbers, bools, and objects are converted to text first.
No. Append changes the builder itself. The return value is only there so that you can chain calls like sb.Append("a").Append("b").
Appending a null string or a null object adds nothing and does not throw. The builder stays as it was.
Number overloads use the current culture, and some cultures use a comma as the decimal separator. Append value.ToString(CultureInfo.InvariantCulture) for output that is the same everywhere.
In a loop, yes, because + creates a new string every time while Append writes into one buffer. For a few fixed pieces, + is fine and easier to read.