How to Convert a Generic List to a Comma-Delimited List

The Situation:

You have a List<string> and need to convert it to a comma-delimited list (for display or storage).

A Solution:

This is a great example of how C# has improved over the years. I found this solution in this StackOverflow discussion and don’t want to lose it, so I am copying it here for reference.

List<string> strings = <code>...;

// .NET 2.0:
string joined = string.Join(",", new List<string>(strings).ToArray());

// .NET 3.5:
string joined = string.Join(",", strings.ToArray());

// .NET 4.0:
string joined = string.Join(",", strings);

Thanks to Jon Skeet, Xavier Poinas and the rest of the guys that participated in that discussion. Gotta love StackOverflow!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s