5

I'm trying to convert an IEnumerable<int> to a string in the format #,#,#,... I'm having a terrible time attempting to make a method of this. What is a quick and easy way to handle?

Thanks.

Channing
  • 119
  • 1
  • 11
  • What was the problem with what you've tried? Have you searched ["create comma separated string"](http://stackoverflow.com/q/4884050/284240)? – Tim Schmelter Nov 12 '13 at 20:38

2 Answers2

14

Use String.Join:

string result = string.Join(",", enumerable);
Tim Schmelter
  • 429,027
  • 67
  • 649
  • 891
Servy
  • 197,813
  • 25
  • 319
  • 428
  • 1
    This is only available starting at `.net 4` [check out 3.5](http://msdn.microsoft.com/en-us/library/System.String.Join(v=vs.90).aspx). – allonhadaya Nov 12 '13 at 20:43
  • 1
    @allonhadaya Correct. You'd need to add your own overload, or first convert the sequence into a string array, if you were on an older version. – Servy Nov 12 '13 at 20:45
2

Are you talking about something like:

string.Join(",", e.Select(i=>i.ToString()).ToArray());

i.e., concatenating an enumerable of ints (e in this case)?

Blindy
  • 60,429
  • 9
  • 84
  • 123