How to Convert a List to a Comma Separated String in C#?

In this C# tutorial, I will explain how to convert a list to a comma separated string in C#. There are various methods to convert a list to a comma separated string in C#.

Convert a List to a Comma Separated String in C#

Below are the 3 different methods to convert a list to a comma separated string in c#.

  • Using string.Join() Method
  • Using StringBuilder Class
  • Using for Loop

Using string.Join() Method

The simplest and most straightforward method for converting a list to a comma-separated string is the string.Join() method in C#. Here is a complete code for converting a list to a comma separated string in C#.

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<string> myList = new List<string> { "apple", "banana", "cherry" };
        string result = string.Join(",", myList);
        Console.WriteLine(result);
    }
}

Once you run the above code using Visual Studio, you can see the output, like the screenshot below:

how to convert list to comma separated string in c#

Using StringBuilder Class

If you want more control over the format of the output or if you are dealing with a large list, using the StringBuilder class might be more appropriate. Below is the complete code to convert a list to a comma separated string in C# using the StringBuilder class.

using System;
using System.Collections.Generic;
using System.Text;

class Program
{
    static void Main()
    {
        List<string> myList = new List<string> { "New York", "California", "Chicago" };
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < myList.Count; i++)
        {
            sb.Append(myList[i]);
            if (i < myList.Count - 1)
            {
                sb.Append(",");
            }
        }

        Console.WriteLine(sb.ToString());
    }
}

Once you run the code, you can see the output below:

convert list to comma separated string in c#

Using for Loop

If you prefer the traditional approach, you can use a for loop to iterate over the list and concatenate the strings with commas. Here is the complete code to convert a list to a comma separated string in C#.

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<string> myList = new List<string> { "Chicago", "San Francisco", "Dallas" };
        string result = "";

        for (int i = 0; i < myList.Count; i++)
        {
            result += myList[i];
            if (i < myList.Count - 1)
            {
                result += ",";
            }
        }

        Console.WriteLine(result);
    }
}

Once you run the code, you can see the output below in the screenshot.

C# convert list to comma separated string

Conclusion

In this C# tutorial, I have explained how to convert a list to a comma-separated string in C#. We explored three primary techniques for converting a list to comma separated string in C#: using string.Join, employing StringBuilder, and utilizing a traditional for loop.

You may also like: