Follow up to this post.
I found
this answer on StackOverflow this morning, which shows an even clearer way to concatenate delimited strings. I'm surprised I hadn't thought of this myself. The first part of that answer, while clever, isn't really important to the discussion. It turns a DataReader into an
IEnumerable<string>. For my purpose it's better to assume we've already come that far. So given the IEnumerable, all you need is this (shown as an extension method):
public static string JoinWith(this IEnumerable<string> strings, string separator)
{
return String.Join(separator, strings.ToArray());
}
Note that it might mean an extra iteration of the enumerable, because you must iterate to produce the array and iterate again to produce the string. It also means you will need the array, where with the other methods you may only ever need produce the completed string. But for many situations the improved clarity is worth it.
Of course the other thing to take away from this is that once you enter the realm of extension methods you could just as easily use them to hide away the (faster) code used in the previous post as well.