Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

C# String Join: Syntax, Overloads, and Practical Usage

Learn how to use string.Join in C# to concatenate arrays and collections, choose the right overload, handle null elements, and avoid common pitfalls.

~4 min readC#
#C##String.Join#StringBuilder#LINQ
Illustration of C# string.Join concatenating an array of strings with a comma separator

When you need to produce a comma-separated list from an array, string.Join is the most direct tool in C#. This method takes a separator and a sequence of strings, then returns a single concatenated string. For example:

csharp
string[] names = { "Alice", "Bob", "Carol" }; string result = string.Join(", ", names); // result: "Alice, Bob, Carol"

This method is part of the .NET base class library and provides several overloads that accept arrays, IEnumerable<T>, and other collection types. This article covers the overloads and practical usage of string.Join, including selection criteria, common mistakes, and performance considerations.

Understanding the string.Join Overloads

string.Join has multiple overloads, each designed for a specific input type. The most commonly used are:

  • string.Join(string separator, params string[] value) – joins an array of strings.
  • string.Join(string separator, IEnumerable<string> values) – joins any sequence of strings, such as List<string> or a LINQ result.
  • string.Join<T>(string separator, IEnumerable<T> values) – joins a sequence of objects, calling ToString() on each element.
  • string.Join(char separator, params string[] value) and string.Join(char separator, IEnumerable<string> values) – accept a char separator instead of a string.

Some target frameworks do not include the char separator overloads. If you are targeting .NET Framework, use the string separator overload or verify the overload list for your target.

The generic overload is particularly useful when you have a collection of non-string types, like integers or custom objects. For example:

csharp
List<int> numbers = new List<int> { 1, 2, 3 }; string result = string.Join(", ", numbers); // result: "1, 2, 3"

Here, the compiler infers T as int and calls int.ToString() for each element. This avoids manual Select calls when you simply need a readable representation.

Joining Arrays, Lists, and Other Collections

The most common use case is joining an array or list of strings. The method works with any IEnumerable<string>, so you can pass a List<string>, a HashSet<string>, or the result of a LINQ query directly.

csharp
List<string> words = new List<string> { "one", "two", "three" }; string joined = string.Join(" - ", words); // joined: "one - two - three"

You can also join a filtered sequence without creating an intermediate list:

csharp
string[] files = Directory.GetFiles(@"C:\temp"); string csv = string.Join(",", files.Where(f => f.EndsWith(".txt")));

Because string.Join consumes the sequence directly, this avoids a separate List<string> or array just for formatting.

Choosing a Separator: String vs Char

If your separator is a single character, you can use the char overload for the convenience of passing a character literal:

csharp
string[] items = { "apple", "banana", "cherry" }; string withChar = string.Join(',', items); // char overload string withString = string.Join(",", items); // string overload

Both produce the same output. Use the string overload when the separator is a longer string or comes from a variable, and the char overload when you are working with a single-character value.

Handling Null and Empty Elements

string.Join does not throw when an element is null; it treats null as an empty string. This behavior is consistent across all overloads. For example:

csharp
string[] values = { "a", null, "b" }; string result = string.Join(",", values); // result: "a,,b"

A null separator is also treated as an empty string, so elements are concatenated directly.

If you need to skip null or empty elements, filter them explicitly before calling string.Join:

csharp
string result = string.Join(",", values.Where(s => !string.IsNullOrEmpty(s)));

This is important when building CSV or log lines where empty fields might cause parsing issues.

Performance Considerations: string.Join vs StringBuilder

A frequent question is whether string.Join is faster than manually building a string with StringBuilder. For simple joins of a known collection, string.Join is usually the better choice. It is designed to avoid the repeated intermediate string allocations that occur when using += in a loop.

StringBuilder is still useful when you need to build a string incrementally with conditional logic, or when append operations are interleaved with other work. For a straightforward join of a collection, string.Join is more readable and avoids a common performance trap.

If you are joining a very large collection, keep in mind that string.Join produces a single string result and therefore needs enough memory for that full result. In rare cases, streaming the output may be preferable.

Common Mistakes and Pitfalls

One common mistake is using a loop with += to concatenate strings; that creates many intermediate strings and can hurt performance. string.Join avoids this problem.

Another pitfall is forgetting that the separator is placed between elements, not after each one. This is usually what you want, but it can be surprising when you need a trailing separator, such as for a CSV header line.

When using the generic overload with custom objects, be aware that ToString() is called on each element. If your object's ToString() returns null, it will be treated as an empty string. Override ToString() if you need a specific representation.

Using string.Join with Non-String Collections

The generic overload is especially handy when you have a collection of numbers or custom types. You can also use LINQ to transform elements before joining:

csharp
List<Product> products = new List<Product>(); // Populate products... string names = string.Join("; ", products.Select(p => p.Name));

This gives you full control over the formatting without writing a loop. For simple cases, the generic overload is sufficient:

csharp
double[] values = { 1.5, 2.5, 3.5 }; string result = string.Join(" | ", values); // result: "1.5 | 2.5 | 3.5"

Because the generic overload calls ToString() on each element, it uses the current culture's default formatting. If you need a specific format, use Select with a format string.

Try matching browser tools

All tools

Small utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.

All C# guides

More in C#

All C# guides

The guides filed next to this one in C#, in library order.