Combining Array Items into String

It’s always useful to be able to combine array items into a comma delimited string whether it’s for output or just debugging the contents of an array or list. C# introduced a helper method to do just that, but I always seem to forget the method and how it works. So, I’m writing this down for my future self and for anyone else that may find it useful.

To convert an array into a comma-delimited string in C#, you can use the string.Join method, similar to converting an array into a space-delimited string. The string.Join method will concatenate the elements of an array, using the specified separator between each element.

Here’s how you can do it:

Example with an Array of Strings

using System;

class Program
{
    static void Main()
    {
        string[] words = { "apple", "banana", "cherry", "date" };
        string result = string.Join(", ", words);
        Console.WriteLine(result);
    }
}

Output:

apple, banana, cherry, date

Example with an Array of Integers

using System;

class Program
{
    static void Main()
    {
        int[] numbers = { 1, 2, 3, 4, 5 };
        string result = string.Join(", ", numbers);
        Console.WriteLine(result);
    }
}

Output:

1, 2, 3, 4, 5

Explanation:

  1. Using string.Join with a Comma Separator:
    • string.Join(", ", words) takes the array words and concatenates its elements into a single string, with a comma and space (", ") between each element. And it is sure to not apply a comma to the end of the list.
  2. Works with Different Data Types:
    • The string.Join method works with any type of array, including strings, integers, and other data types that can be converted to a string. When working with an object, it defaults to calling that object’s ToString method.
  3. Handling Null Values:
    • If the array contains null values, string.Join treats them as empty strings. For example, if the array contains {"apple", null, "cherry"}, the resulting string would be "apple, , cherry".

To show using the Join method with a complex type, see the code below. We introduce the Person class which we keep in a list, and then want to display that list as a string.

using System;
using System.Collections.Generic;
using System.Linq;

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }

    public override string ToString()
    {
        return $"{Id}: {Name}";
    }
}

class Program
{
    static void Main()
    {
        // Create a list of Person objects
        List<Person> people = new List<Person>
        {
            new Person { Id = 1, Name = "Alice" },
            new Person { Id = 2, Name = "Bob" },
            new Person { Id = 3, Name = "Charlie" }
        };

        // Convert the list of Person objects to a comma-delimited string
        string result = string.Join(", ", people);
        string result2 = string.Join(", ", people.Select(p => $"{p.Id}: {p.Name}"));

        // Output the result
        Console.WriteLine(result);
        Console.WriteLine(result2);
    }
}

The code above prints out the id and name of each person. The first Join operation just passes the people list, so the ToString implementation is implicitly called on each person object. The second Join operation uses LINQ to project the resulting string for each object in the list. Both produce the exact same output, but your use depends on the needs of our code.

    Output:

    1: Alice, 2: Bob, 3: Charlie
    1: Alice, 2: Bob, 3: Charlie
    

    Hopefully you found this explanation useful. I’ve had to look it up too many times, so decided to place it here for my own future reference.

    Leave a comment