How do I create a comma delimited string from an ArrayList?

Yes, I’m answering my own question, but I haven’t found it here yet and thought this was a rather slick thing:

…in VB.NET:

String.Join(",", CType(TargetArrayList.ToArray(Type.GetType("System.String")), String()))

…in C#

string.Join(",", (string[])TargetArrayList.ToArray(Type.GetType("System.String")))

The only “gotcha” to these is that the ArrayList must have the items stored as Strings if you’re using Option Strict to make sure the conversion takes place properly.

EDIT: If you’re using .net 2.0 or above, simply create a List(Of String) type object and you can get what you need with. Many thanks to Joel for bringing this up!

String.Join(",", TargetList.ToArray())

Leave a Comment