In C#, StringBuilder.AppendJoin(separator, values) adds every value to the end of the builder, with the separator placed between them (never before the first or after the last). It is the builder version of string.Join.
You could loop and call Append() yourself, but then you must skip the last separator. AppendJoin does that for you, and writes into the builder directly instead of making a temporary joined string.
An instance method of StringBuilder (using System.Text;), called as sb.AppendJoin(...). The builder is a mutable buffer that changes in place, unlike an immutable string. It is not thread-safe. AppendJoin exists in .NET Core 2.0 and later, .NET 5+, and .NET Standard 2.1. It is not in .NET Framework; there, use sb.Append(string.Join(sep, items)).
Syntax
Pass the separator (a string or a char) and the values: an array, a params list, or any IEnumerable<T>.
C#
StringBuilder sb = new StringBuilder();
sb.AppendJoin(", ", "a", "b", "c"); // a, b, c
sb.AppendJoin('/', new[] { "x", "y" }); // a, b, cx/y
List<int> nums = new List<int> { 1, 2 };
sb.AppendJoin("+", nums); // a, b, cx/y1+2
Call
What it does
sb.AppendJoin(", ", array)
Adds the array items with ", " between them.
sb.AppendJoin('-', "a", "b")
A char separator works too: a-b.
sb.AppendJoin(" | ", list)
Any IEnumerable<T>, such as a List<int>. Each item is converted with ToString().
sb.AppendJoin(";", emptyArray)
No items means nothing is added, not even a separator.
A null item is treated as an empty string, so { "a", null, "b" } joined with ";" gives a;;b. A null separator is treated as empty too. Passing a null array or sequence itself throws ArgumentNullException.
Library declarations
C#
public StringBuilder AppendJoin(string? separator, params string?[] values);
public StringBuilder AppendJoin(string? separator, params object?[] values);
public StringBuilder AppendJoin<T>(string? separator, IEnumerable<T> values);
public StringBuilder AppendJoin(char separator, params string?[] values);
public StringBuilder AppendJoin(char separator, params object?[] values);
public StringBuilder AppendJoin<T>(char separator, IEnumerable<T> values);
Parameters
Parameter
Description
separator
The text or single char placed between items. It is not added before the first item or after the last one.
values
The items to add: an array, a comma-separated params list, or any sequence. Non-string items are converted with ToString().
Return Value
Return Value
Description
StringBuilder
The same builder, with the joined items added at the end.
C# Examples
Example 1 — Join an Array After a Label
Start the builder with a label, join three fruit names after it, then build a path with a char separator.
C#
using System;
using System.Text;
class Program {
static void Main() {
string[] fruits = { "apple", "banana", "cherry" };
StringBuilder sb = new StringBuilder("Fruits: ");
sb.AppendJoin(", ", fruits);
Console.WriteLine(sb.ToString());
StringBuilder path = new StringBuilder();
path.AppendJoin('/', "home", "user", "docs");
Console.WriteLine(path.ToString());
}
}
Output
Fruits: apple, banana, cherry
home/user/docs
How It Works
Existing text stays. The builder already holds Fruits:, and AppendJoin adds the joined list after it.
Separators only between. Three items need two separators, so the text ends with cherry and no trailing comma.
A char separator.AppendJoin('/', ...) uses a single '/' character between the three parts listed right in the call.
No manual loop. Both lines are built without a for loop or a "skip the last comma" check.
Example 2 — Join a List of Numbers and Mixed Values
Join a List<int> inside square brackets in one chained statement, then join values of different types.
C#
using System;
using System.Collections.Generic;
using System.Text;
class Program {
static void Main() {
List<int> scores = new List<int> { 90, 75, 88 };
StringBuilder sb = new StringBuilder();
sb.Append('[').AppendJoin(" | ", scores).Append(']');
Console.WriteLine(sb.ToString());
sb.Clear();
sb.AppendJoin(", ", "id", 42, true);
Console.WriteLine(sb.ToString());
}
}
Output
[90 | 75 | 88]
id, 42, True
How It Works
Generic overload. A List<int> is an IEnumerable<int>, so the AppendJoin<T> overload is picked and each number is turned into text.
Chained brackets.Append('['), AppendJoin, and Append(']') all return the builder, so the whole line is one statement.
Mixed types."id", 42, and true together match the params object[] overload. Each value is converted with ToString(), so the bool prints as True.
Reset between jobs.Clear() removes the bracket line first, so the second output is only the mixed list.
Example 3 — Null Items, an Empty Array, and string.Join
See how a null item and an empty array behave, and check that the result matches string.Join.
C#
using System;
using System.Text;
class Program {
static void Main() {
string[] parts = { "red", null, "blue" };
StringBuilder sb = new StringBuilder();
sb.AppendJoin(";", parts);
Console.WriteLine(sb.ToString());
Console.WriteLine(sb.Length);
Console.WriteLine(sb.ToString() == string.Join(";", parts));
sb.Clear();
sb.AppendJoin(";", Array.Empty<string>());
Console.WriteLine(sb.Length);
}
}
Output
red;;blue
9
True
0
How It Works
A null item is empty. The middle item is null, so nothing is written for it, but both separators around it remain: red;;blue.
Length adds up. 3 letters for red, 2 semicolons, and 4 letters for blue make 9 characters.
Same as string.Join.string.Join(";", parts) builds the very same text, so the comparison is True. The builder just skips the extra string.
Nothing to join. An empty array adds no items and no separator, so after Clear() the length stays 0.
Frequently Asked Questions
It adds all the items of an array or sequence to the end of the builder, with a separator between each pair of items. It returns the same builder.
No. It exists in .NET Core 2.0 and later, .NET 5 and later, and .NET Standard 2.1. On .NET Framework use sb.Append(string.Join(separator, items)).
The joined text is the same. string.Join returns a new string, while AppendJoin writes the text straight into a StringBuilder, after anything already in it.
A null item is treated as an empty string. The separators around it are still added, so "a", null, "b" with ";" gives a;;b.
Yes. There are overloads that take a char separator, such as sb.AppendJoin(',', items), as well as overloads that take a string.