Comma Delimiting Strings in .NET – The Framework Way
Stop using StringBuilder to join comma-separated strings in .NET - the built-in CommaDelimitedStringCollection class does it for you.
Donn Felker
2007.11.15 · 1 min read
It’s common to see a developer use a StringBuilder or basic string concatenation to concatenate strings in .NET code.
You’ll usually see something like this:
stringcomma=",";
StringBuildersb=newStringBuilder();
sb.Append("Donn");
sb.Append(comma);
sb.Append("Felker");
sb.Append(comma);
sb.Append("Jiu-Jitsu");
sb.ToString();
And this will return “Donn,Felker,Jiu-Jitsu”
But the .NET framework has something built in just for doing this type of operation.
Its called the CommaDelimitedStringCollection and it’s located in System.Configuration namespace. What it allows you to do is to add items to a collection and then just access the “ToString” method of the class to return the value of the collection, delimited by comma’s.
I've spent 25+ years shipping software, writing books, and running my own companies. I write about thinking clearly, working for yourself, and doing real work while AI rewrites the rules. New essays land here every week.