Convert array of integers to comma-separated string


C# Problem Overview

It's a simple question; I am a newbie in C#, how can I perform the following

  • I want to convert an array of integers to a comma-separated string.

I have

int[] arr = new int[5] {1,2,3,4,5};

I want to convert it to one string

string => "1,2,3,4,5"

C# Solutions

Solution 1 - C#

var result = string.Join(",", arr);

This uses the following overload of string.Join:

public static string Join<T>(string separator, IEnumerable<T> values);

Solution 2 - C#

.NET 4

string.Join(",", arr)

.NET earlier

string.Join(",", Array.ConvertAll(arr, x => x.ToString()))

Solution 3 - C#

int[] arr = new int[5] {1,2,3,4,5};

You can use Linq for it

String arrTostr = arr.Select(a => a.ToString()).Aggregate((i, j) => i + "," + j);

Solution 4 - C#

You can have a pair of extension methods to make this task easier:

public static string ToDelimitedString<T>(this IEnumerable<T> lst, string separator = ", ")
    return lst.ToDelimitedString(p => p, separator);

public static string ToDelimitedString<S, T>(this IEnumerable<S> lst, Func<S, T> selector, 
                                             string separator = ", ")
    return string.Join(separator, lst.Select(selector));

So now just:

new int[] { 1, 2, 3, 4, 5 }.ToDelimitedString();

Solution 5 - C#

Use LINQ Aggregate method to convert array of integers to a comma separated string

var intArray = new []{1,2,3,4};
string concatedString = intArray.Aggregate((a, b) =>Convert.ToString(a) + "," +Convert.ToString( b));

output will be


This is one of the solution you can use if you have not .net 4 installed.


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHaim EvgiView Question on Stackoverflow
Solution 1 - C#Cheng ChenView Answer on Stackoverflow
Solution 2 - C#leppieView Answer on Stackoverflow
Solution 3 - C#Manish NayakView Answer on Stackoverflow
Solution 4 - C#nawfalView Answer on Stackoverflow
Solution 5 - C#sushil pandeyView Answer on Stackoverflow