How to convert IEnumerable<string> to one comma separated string?

Johann Gerell picture Johann Gerell · Sep 20, 2011 · Viewed 57.4k times · Source

Say that for debugging purposes, I want to quickly get the contents of an IEnumerable into one-line string with each string item comma-separated. I can do it in a helper method with a foreach loop, but that's neither fun nor brief. Can Linq be used? Some other short-ish way?

Answer

한국인 picture 한국인 · Sep 20, 2011
using System;
using System.Collections.Generic;
using System.Linq;

class C
{
    public static void Main()
    {
        var a = new []{
            "First", "Second", "Third"
        };

        System.Console.Write(string.Join(",", a));

    }
}