Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Strings and Text

C# String.Join() and String.Concat() methods

Reference for C# string.Join() and string.Concat(): join a sequence with a separator, concatenate values, handle nulls, and avoid slow loops of +.

string.Join glues the elements of a sequence into one string, putting a separator between neighbors. string.Concat does the same without a separator, and it also accepts a plain list of values. Both are static methods and both build the result in a single allocation, so they are much cheaper than appending in a loop with +=.

Elements that are null become empty strings. Elements that are not strings are converted with ToString() using the current culture, which in our runner is the invariant culture.

C#
string csv = string.Join(", ", new[] { "red", "green", "blue" });
string joined = string.Concat("a", "b", "c");

Try it

C# Join and Concat basics

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Nulls, empty input and errors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
public static string Join(string? separator, IEnumerable<string?> values)
public static string Join(string? separator, params string?[] value)
public static string Join(string? separator, params object?[] values)
public static string Join<T>(string? separator, IEnumerable<T> values)
public static string Join(char separator, params string?[] value)
public static string Join(string? separator, string?[] value, int startIndex, int count)

public static string Concat(params string?[] values)
public static string Concat(params object?[] args)
public static string Concat(string? str0, string? str1)
public static string Concat<T>(IEnumerable<T> values)

There are also char separator versions for the object and generic overloads, and in recent .NET versions params ReadOnlySpan<string?> overloads that avoid allocating an array for the call.

Name Description
separator Text (or one char) inserted between elements. null is treated as an empty string.
values / value The elements to join. Non-string elements are converted with ToString().
startIndex, count For the array overload: the first element to use and how many to take.

Returns: the joined string. An empty sequence gives "".

Errors / undefined behavior: ArgumentNullException if the sequence itself is null. ArgumentOutOfRangeException if startIndex or count is negative or goes past the end of the array.

Usage notes

  • Separator goes between, not after. Join(", ", ...) never leaves a trailing separator, which is why it beats manual loops.
  • null elements are not skipped. They become empty strings, so Join(",", "a", null, "c") gives a,,c with the separator still present. Filter with Where(x => x != null) first if you want them gone.
  • Prefer Join over += in loops. Each += copies the whole string so far, giving quadratic time. Join measures once and copies once. For incremental building with branching logic, use StringBuilder.
  • Any IEnumerable<T> works. Integers, enums, and your own types are joined through ToString(). The sequence is enumerated once.
  • Format first when you need control. Join(", ", prices.Select(p => p.ToString("F2", CultureInfo.InvariantCulture))) formats each price before joining.
  • Concat versus +. a + b + c on strings compiles to Concat(a, b, c), so both are the same thing. Use Concat for arrays or sequences.
  • Opposite operation: Split cuts a string at separators. Split followed by Join is a common way to normalize delimiters.
  • Joining with a final "and" is not built in; join all but the last element and add the last by hand.