What is the cleanest way to create a comma-separated list of string values from an IList<string> or IEnumerable<string>?
String.Join(...) operates on a string[] so can be cumbersome to work with when types such as IList<string> or IEnumerable<string> cannot easily be converted into a string array.
.NET 4+
Detail & Pre .Net 4.0 Solutions
IEnumerable<string>can be converted into a string array very easily with LINQ (.NET 3.5):It’s easy enough to write the equivalent helper method if you need to:
Then call it like this:
You can then call
string.Join. Of course, you don’t have to use a helper method:The latter is a bit of a mouthful though 🙂
This is likely to be the simplest way to do it, and quite performant as well – there are other questions about exactly what the performance is like, including (but not limited to) this one.
As of .NET 4.0, there are more overloads available in
string.Join, so you can actually just write:Much simpler 🙂