How to Convert an Array to a Comma-Separated String in C#?

This is a very common requirement for C# developers: how to convert an array to a comma separated string in C#.

Below are the different methods to convert an array to a comma-separated string in C#.

  1. Using a For Loop
  2. Using String.Join()
  3. Using StringBuilder
  4. Using LINQ

Convert array to comma separated string c#

We will see all 4 methods with complete examples with code you can run using a Visual Studio console application.

1. Using a For Loop

One of the simplest ways to convert an array to a comma-separated string is by using a for loop in C#. Here is the complete code:

using System;

namespace ArrayToCommaSeparatedString
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize an array with U.S. city names
            string[] cities = { "New York", "Chicago", "San Francisco" };

            // Declare a variable to hold the resulting comma-separated string
            string result = "";

            // Loop through the array to concatenate the city names
            for (int i = 0; i < cities.Length; i++)
            {
                result += cities[i];

                // Add a comma and space, except after the last element
                if (i < cities.Length - 1)
                {
                    result += ", ";
                }
            }

            // Print the resulting comma-separated string
            Console.WriteLine(result);
        }
    }
}

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

how to convert array to comma separated string c#

The pros of this method is that, it is a simple and straightforward approach, but it is not the most efficient for large arrays.

2. Using String.Join()

The C# String.Join() method offers a more modern and efficient approach for converting an array to a comma-separated string. So this is another useful method you can use to convert an array to comma separated string c#.

using System;

namespace UsingStringJoin
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize an array with U.S. city names
            string[] cities = { "New York", "Chicago", "San Francisco" };

            // Use String.Join() to create a comma-separated string
            string result = String.Join(", ", cities);

            // Print the resulting comma-separated string
            Console.WriteLine(result);
        }
    }
}

3. Using StringBuilder

If you are dealing with a large array, then you can use StringBuilder to convert an array to comma separated string c#. This is slightly more complex than other methods but efficient for large arrays. Here is the complete code.

using System;
using System.Text;

namespace UsingStringBuilder
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize an array with U.S. city names
            string[] cities = { "New York", "Chicago", "San Francisco" };

            // Use StringBuilder for concatenation
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < cities.Length; i++)
            {
                sb.Append(cities[i]);

                // Add a comma and space, except after the last element
                if (i < cities.Length - 1)
                {
                    sb.Append(", ");
                }
            }

            // Convert StringBuilder to string
            string result = sb.ToString();

            // Print the resulting comma-separated string
            Console.WriteLine(result); 
        }
    }
}

4. Using LINQ

We can also use LINQ to convert an array to a comma-separated string in C#. Here is the complete code.

using System;
using System.Linq;

namespace UsingLINQ
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize an array with U.S. city names
            string[] cities = { "New York", "Chicago", "San Francisco" };

            // Use LINQ to create a comma-separated string
            string result = string.Join(", ", cities.Select(x => x.ToString()));

            // Print the resulting comma-separated string
            Console.WriteLine(result);
        }
    }
}

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

convert array to comma separated string c#

Conclusion

This complete tutorial explored 4 methods to convert an array to a comma-separated string in C#.

  1. Using a For Loop
  2. Using String.Join()
  3. Using StringBuilder
  4. Using LINQ

You may also like: