DelimitedList<T> – a useful utility class

There are several classes which I have written over the years which have become almost like my own personal BCL when developing .Net projects. I think most people have these classes laying around in some form or fashion. In fact, I have joined a formalized effort led by Steve Wright to make an open source version of this “Toolbox” called nToolbox. While that project is getting off the ground, I figured I might share some of the classes which have been very useful to me personally. If this topic interests you, you can read my previous post where I shared some code which I use to find Controls on an Asp.Net page.


Here is the situation I kept finding myself in – I will use the case of a comma delimited list of items since it is the most common scenario:

I want to loop through a list of strings and build a comma delimited list. The code begins something like this:

string[] items = new string[] 

StringBuilder list = new StringBuilder();       

foreach (string item in items) 
    list.AppendFormat("{0},", item); 


The problem with this is it will produce the following string:


Notice that there is a trailing comma on the end. So then I end up having to explicitly remove that last comma by either not adding it on the last iteration through the loop or by removing it after the loop has completed.


So I wrote a class which will help me produce such lists but without the pain of remembering if there is a comma at the end or not. Although the DelimitedList class defaults to just having a single comma as its delimited, it can actually have any string as a delimited. I have used it in the past to delimited things by pipes ‘|’ and even line breaks which can be useful when you want one item per line.

Rewriting the same code above with the delimited list would look like this:

DelimitedList<string> list = new DelimitedList<string>();       

foreach (string item in items) 

Now this will produce the expected string:

The DelimitedList can also work on any type of object (thus the use of Generics) so you can use it with types other than strings: (just for varieties sake, I will use a delimiter other than a comma)

DelimitedList<int> list = new DelimitedList<int>("+");       

for (int i = 0; i < 10; i++) 


This will produce the following string:


You can download the complete code for the DelimitedList class here


3 Responses to DelimitedList<T> – a useful utility class

  1. Michael says:

    Nice thing, but indeed, mostly we are able to do similar tasks the standard .NET way.

    1) You can conveniently do String.Join(“,”, myArray);

    2) There is the System.Configuration.CommaDelimitedStringCollection

  2. Luke Foust says:

    I should have mentioned the merits of doing this type of thing using strictly BCL classes. I was not aware of the CommaDelimitedStringCollection so I may consider using that in some cases. Overall, I still prefer the flexibility of the DelimitedList class I wrote. I do, however, not like having to rely on my own utility classes for common tasks. Thank you for your feedback.

  3. […] associated to enums as well as some parsing methods similar to these. I have written about DelimitedList and the state managed classes before. I make heavy use of QueryStringHelper to build the query […]

Leave a Reply

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

You are commenting using your 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: