3

What is the best way to convert list of Int32 to a string with a separator like ',' in C#?

M_Mogharrabi
  • 1,369
  • 5
  • 29
  • 57
  • 3
    possible duplicate http://stackoverflow.com/questions/4981390/how-to-convert-list-to-string-in-c –  Oct 02 '12 at 08:32

4 Answers4

7

You can use string.Join:

var intList = new[] { 1, 2, 3, 4, 5 };
var result = string.Join(",", intList);

Edit:

If you are from .NET 4.0, string.Join accepts input parameter as IEnumerable<T>, so you don't need to convert to Array by ToArray.

But if you are in .NET 3.5: like other answers, ToArray should be used.

cuongle
  • 74,024
  • 28
  • 151
  • 206
3
string Result = string.Join(",", MyList.ToArray());
bAN
  • 13,375
  • 16
  • 60
  • 93
3

Join on a string: String.Join(",", list.ToArray());

Lewis Harvey
  • 332
  • 2
  • 10
2
string commaSeparated = String.Join(",", Intlist.ToArray());
CloudyMarble
  • 36,908
  • 70
  • 97
  • 130