How to create comma separated list in C#?

Very often during development you need somekind of coma or pipe separated string. Since .NET 1.0, framework had string.Join method. So, you can do something like this:

string[] a = new string[] { "Viktar", "Vasya", "Ivan" };           

Response.Write(string.Join(",", a));

Howerver, this not really useful, since you usually doesn't have string array. Usually you have list or array of some kind of objects. Let me show you how you can do it .NET 3.5 using LINQ:

List<Person> persons = new List<Person>();

persons.Add(new Person { FirstName = "Viktar", LastName = "Karpach" });

persons.Add(new Person { FirstName = "Vasya", LastName = "Pupkin" });

persons.Add(new Person { FirstName = "Ivan", LastName = "Ivanov" });                       

Response.Write(string.Join(",", (from p in persons select p.FirstName).ToArray()));


Posted on Wednesday, January 14, 2009 by | Comments (12) | Add Comment

Comments

Gravatar

Re:How to create comma separated list in C#?

Wow! Cool! simple but useful!

Posted on 6/23/2009 2:03:29 AM by Chris #
Gravatar

Re:How to create comma separated list in C#?

What if words may contain commas?

Posted on 7/10/2009 8:58:22 PM by leve #
Gravatar

Doesn't matter. You are producing list, not parsing it.

Gravatar

Re:How to create comma separated list in C#?

Really useful. Saved my time from writing custom method. Thank U :)

Posted on 9/3/2009 5:42:46 AM by Krishnaraj #
Gravatar

Re:How to create comma separated list in C#?

Thanks, simple and useful.

Posted on 9/7/2009 7:13:12 PM by jose pulido #
Gravatar

Re:How to create comma separated list in C#?

Grt8..Save my time too...one google search

Posted on 11/2/2009 8:40:15 AM by Nasir #
Gravatar

Re:How to create comma separated list in C#?

perfect :)
thanks

Posted on 7/15/2010 12:08:40 AM by kazim #
Gravatar

Re:How to create comma separated list in C#?

Superb..

Posted on 5/12/2011 9:46:10 PM by Roy #
Gravatar

Re:How to create comma separated list in C#?

Great - I figured there should be a nice way to achieve what I needed, and here it is :) Thanks!!

Posted on 8/23/2011 7:33:24 PM by Jen #
Gravatar

Re:How to create comma separated list in C#?

Worked like a charm! Thanks!

Posted on 9/22/2011 4:25:11 AM by Immalee #
Gravatar

Re:How to create comma separated list in C#?

used it...save me some time - thanks for the tip!

Posted on 10/18/2011 7:20:45 AM by Doug #
Gravatar

Re: How to create comma separated list in C#?

how can i come up to an output like this

3,4,5,6 and 7

Posted on 7/26/2012 3:16:01 AM by zylfa #
Gravatar

Re: How to create comma separated list in C#?

Perfect! Saved me some time..

Posted on 10/18/2012 11:47:33 AM by sykespro #

New Comment

Your Name:
Email (for internal use only):
Comment:
 
Code above:

Categories

Recent Tweets

  • Simon Ince's Blog: Hierarchies with HierarchyID in SQL 2008 http://t.co/xSDwiF6rRS.
  • Visual Studio 2010 WAS painfully slow - CodeProject http://t.co/Usba1x6CZy

Valid HTML5