Question

Convert List<string> to a Delimited String in C#

csharpstringlistdelimiter

Question

In C#, is there a built-in way to quickly convert a collection into a single string with a delimiter between values?

For example, given a list like this:

List<string> names = new List<string> { "John", "Anna", "Monica" };

How can I produce a string like this?

string namesTogether = "John, Anna, Monica";

Short Answer

By the end of this page, you will understand how to combine a List<string> or other collections into one string in C#. You will learn the standard method, string.Join, how it works, when to use it, and common mistakes to avoid.

Concept

In C#, the standard way to combine many string values into one string with a separator is string.Join.

When you have a collection such as:

List<string> names = new List<string> { "John", "Anna", "Monica" };

and want this result:

"John, Anna, Monica"

string.Join is the built-in tool designed for exactly that job.

Example:

string result = string.Join(", ", names);

This produces:

John, Anna, Monica

Why this matters:

  • You often need to display lists of values to users.
  • You may need to create CSV-like text output.
  • You may need to build log messages, API parameters, or configuration values.
  • It is cleaner and safer than manually looping and adding separators yourself.

Instead of writing custom code to add commas between items and avoid an extra comma at the end, string.Join handles that for you.

Mental Model

Think of string.Join like a machine that takes:

  • a separator such as ", "
  • a list of items

and places the separator between each item.

If the items are:

  • John
  • Anna
  • Monica

and the separator is ,

the machine builds:

John, Anna, Monica

A helpful detail: the separator goes between items, not before the first one or after the last one. That is why string.Join is so useful.

Take Quiz

Syntax and Examples

The basic syntax is:

string.Join(separator, collection)

Example with List<string>

List<string> names = new List<string> { "John", "Anna", "Monica" };
string namesTogether = string.Join(", ", names);

Console.WriteLine(namesTogether);

Output:

John, Anna, Monica

Explanation:

  • ", " is the delimiter.
  • names is the collection.
  • string.Join returns one combined string.

Example with an array

string[] colors = { "Red", "Green", "Blue" };
string result = string.Join(" | ", colors);

Console.WriteLine(result);

Output:

Red | Green | Blue

Example with numbers

string.Join also works with other types because values can be converted to strings.

List<int> numbers = new List<int> { 10, 20, 30 };
string result = string.Join("-", numbers);

Console.WriteLine(result);

Output:

10-20-30

Example with filtering before joining

List<string> names = new List<string> { "John", "", "Anna", "Monica" };
string result = string.Join(", ", names.Where(name => !string.IsNullOrWhiteSpace(name)));

Console.WriteLine(result);

Output:

John, Anna, Monica

This is useful when you want to skip empty values before joining them.

Step by Step Execution

Consider this code:

List<string> names = new List<string> { "John", "Anna", "Monica" };
string result = string.Join(", ", names);
Console.WriteLine(result);

Step by step:

  1. A List<string> is created with three values:

    • John
    • Anna
    • Monica
  2. string.Join(", ", names) is called.

  3. C# starts with the first item:

    • current result: John
  4. It sees another item exists, so it inserts the separator , :

    • current result: John,
  5. It adds the next item Anna:

    • current result: John, Anna
  6. It inserts the separator again before the next item:

    • current result: John, Anna,
  7. It adds Monica:

    • current result: John, Anna, Monica
  8. The final string is returned and stored in result.

  9. Console.WriteLine(result) prints:

John, Anna, Monica

Notice that no extra comma is added at the end. That is one of the main benefits of using string.Join.

Real World Use Cases

Here are common places where this concept is used:

Displaying names or tags in a UI

List<string> tags = new List<string> { "csharp", "dotnet", "backend" };
string display = string.Join(", ", tags);

Result:

csharp, dotnet, backend

Writing log messages

List<string> errors = new List<string> { "Missing email", "Invalid password" };
string message = "Validation failed: " + string.Join("; ", errors);

Building CSV-like lines

string[] fields = { "John", "Developer", "London" };
string csvLine = string.Join(",", fields);

Creating query parameter values

List<int> ids = new List<int> { 101, 102, 103 };
string idList = string.Join(",", ids);

This can be useful when an API expects something like:

ids=101,102,103

Creating readable summaries

List<string> completedTasks = new List<string> { "Login", "Search", "Checkout" };
string summary = "Completed: " + string.Join(", ", completedTasks);
Take Quiz

Real Codebase Usage

In real projects, developers often use string.Join together with filtering, mapping, and validation.

Pattern: filter before join

var parts = new List<string> { firstName, middleName, lastName };
string fullName = string.Join(" ", parts.Where(p => !string.IsNullOrWhiteSpace(p)));

This avoids extra spaces when one part is missing.

Pattern: map values before join

var users = new List<string> { "john", "anna", "monica" };
string result = string.Join(", ", users.Select(u => u.ToUpper()));

This transforms each item before combining it.

Pattern: join validation errors

var errors = new List<string>();

if (string.IsNullOrWhiteSpace(email))
    errors.Add("Email is required");

if (password.Length < 8)
    errors.Add("Password must be at least 8 characters");

string errorMessage = string.Join("; ", errors);

Pattern: guard against null collections

List<string>? names = GetNames();
string result = string.Join(", ", names ?? new List<string>());

This prevents null-related issues when the collection might not exist.

Pattern: preparing output for configuration or logging

var enabledFeatures = new[] { "Search", "Reports", "Export" };
logger.LogInformation("Enabled features: {Features}", string.Join(", ", enabledFeatures));

In real codebases, string.Join is often part of a larger data-cleaning or formatting pipeline.

Common Mistakes

1. Building the string manually in a loop

Beginners often do this:

string result = "";
foreach (string name in names)
{
    result += name + ", ";
}

Problem:

  • It adds an extra delimiter at the end.
  • Repeated string concatenation is less clean.

Better:

string result = string.Join(", ", names);

2. Forgetting that null items may exist

List<string> names = new List<string> { "John", null, "Anna" };
string result = string.Join(", ", names);

This may produce an empty spot in the output, such as:

John, , Anna

If that is not what you want, filter first:

string result = string.Join(", ", names.Where(n => !string.IsNullOrWhiteSpace(n)));

3. Confusing string.Join with string.Concat

string result = string.Concat(names);

This joins items with no separator.

Output:

JohnAnnaMonica

Use string.Join when you need a delimiter.

4. Not handling a null collection

Broken example:

List<string> names = null;
string result = string.Join(", ", names);

Depending on the situation, this can fail or lead to unwanted behavior. Safer approach:

string result = string.Join(", ", names ?? new List<string>());

5. Using the wrong delimiter

string result = string.Join(",", names);

This gives:

John,Anna,Monica

If you want spaces after commas, use:

string result = string.Join(", ", names);

Comparisons

ApproachWhat it doesBest forExample output
string.Join(", ", items)Combines items with a separatorHuman-readable listsJohn, Anna, Monica
string.Concat(items)Combines items with no separatorDirect concatenationJohnAnnaMonica
Manual loop with +=Builds string piece by pieceUsually not preferred for this taskOften error-prone
StringBuilderEfficient for complex manual string buildingAdvanced formatting logicCustom output

string.Join vs string.Concat

var names = new List<string> { "John", "Anna", "Monica" };

Console.WriteLine(string.Join(", ", names));
Console.WriteLine(string.Concat(names));

Output:

John, Anna, Monica
JohnAnnaMonica

string.Join vs manual loop

string.Join is better when:

  • you need a separator
  • you want clean, readable code
  • you want to avoid extra separators at the end

A manual loop is only worth using when formatting rules are more complicated than a simple delimiter.

Take Quiz

Cheat Sheet

// Basic usage
string result = string.Join(", ", names);

// With array
string result2 = string.Join(" | ", colors);

// With numbers
string result3 = string.Join("-", numbers);

// Skip null or empty values
string result4 = string.Join(", ", names.Where(n => !string.IsNullOrWhiteSpace(n)));

// Safe when collection may be null
string result5 = string.Join(", ", names ?? new List<string>());

Quick rules:

  • Use string.Join(separator, collection) to combine values.
  • The separator goes between items.
  • No extra separator is added at the end.
  • string.Concat does not add separators.
  • Filter values first if you want to remove empty or null items.
  • Use ?? if the collection might be null.

FAQ

What is the easiest way to convert a list of strings to one string in C#?

Use:

string result = string.Join(", ", names);

Does string.Join work with lists and arrays?

Yes. It works with arrays, lists, and other collections.

Can I use string.Join with integers or other types?

Yes. Values can be converted to strings automatically.

How do I remove empty items before joining?

Use LINQ:

string result = string.Join(", ", names.Where(n => !string.IsNullOrWhiteSpace(n)));

What is the difference between string.Join and string.Concat?

string.Join adds a separator between items. string.Concat does not.

Does string.Join add a comma at the end?

No. It only inserts the separator between elements.

What if my list is null?

Use a fallback:

string result = string.Join(", ", names ?? new List<string>());
Take Quiz

Mini Project

Description

Create a small C# program that formats a contact list into a readable string. This demonstrates how to join string values with a delimiter, while also cleaning the data by removing empty entries.

Goal

Build a program that takes a list of names, removes invalid entries, and prints one comma-separated string.

Requirements

  • Create a list of names with at least one empty or null value.
  • Filter out null, empty, or whitespace-only entries.
  • Join the remaining names using ", " as the delimiter.
  • Print the final result to the console.
Take Quiz

Keep learning

Related questions

View more