The Self-Taught Programmer

Recipes from real-world experience with asp.net.

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

leave a comment »

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!

Advertisements

Written by Jim McMullen

March 25, 2013 at 11:32 am

Posted in How-To

Tagged with ,

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 )

Google+ photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s

%d bloggers like this: