Chris Crowe's Blog
Ramblings of an IIS MVP ( MVP Since 1997 )

Powered By IIS 7

Search my blog

Some of my readers



My Microsoft Certifications


Dec 15, 1998

Dec 20, 2000

Jan 31, 2001

Jul 22, 2002

Nov 1, 2004

My Microsoft MVP Awards




1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
mvp.support.microsoft.com

I found an interesting post from a work colleague "Simeon Pilgrim" at http://simeonpilgrim.com/blog/2007/08/23/how-to-rewriter-systemconfigurationcommadelimitedstringcollection-wrong/ that shows that in the System.Configuration namespace there is a very simple class that will create a comma seperated string for you.

The class is called CommaDelimitedStringCollection and you must add a reference to System.Configuration in order to use it.

 
There is a simple example of how to use it.
using System; 
using System.Collections; 
using System.Configuration; 

namespace Commas 
{ 
    internal class Program 
    { 
        private static void Main(string[] args) 
        { 
            ArrayList Items = new ArrayList(); 
            Items.Add("http://blog.crowe.co.nz"); 
            Items.Add("http://www.simeonpilgrim.com"); 
            Items.Add("http://www.iis.net"); 

            CommaDelimitedStringCollection commaStr = 
                new CommaDelimitedStringCollection(); 

            foreach (string item in Items) 
                commaStr.Add(item); 

            Console.WriteLine(commaStr.ToString()); 
        } 
    } 
}
The output from the above would be :

http://blog.crowe.co.nz,http://www.simeonpilgrim.com,http://www.iis.net

In the sample above we loop through each item in my ArrayList and add it to the list of strings to comma seperate.
We could have replaced for foreach loop with this to make it a bit easier.

commaStr.AddRange((string[]) Items.ToArray(typeof(string)));

 

I wonder how many other usefull classes are out there but no one uses them and reinvents the wheel.

posted on Saturday, August 25, 2007 9:26 AM | Filed Under [ c# ]

Comments


# re: c# - How to create a comma seperated string from a list of items

Gravatar
We reinvent the wheel because we are selfish and lazy. We like to have our own things and we do not like to search for ready to use things.
Posted by WEB Security Man on 5/8/2008 10:46 PM

# re: c# - How to create a comma seperated string from a list of items

Gravatar
Reinventing the wheel is human passion.
Posted by .NET File Explorer Developer on 2/10/2009 1:06 AM

# re: c# - How to create a comma seperated string from a list of items


Could you do the same thing with string.Join(",", yourString) ?

Seems to me like Microsoft reinvents their own wheels.
Posted by Interest Developer on 2/18/2009 8:09 AM

Post Comment


Title *
Name *
Email
Url
Comment *  
Please add 8 and 6 and type the answer here: