Eric Lippert’s challenge “comma-quibbling”, best answer?

Inefficient, but I think clear.

public static string CommaQuibbling(IEnumerable<string> items)
{
    List<String> list = new List<String>(items);
    if (list.Count == 0) { return "{}"; }
    if (list.Count == 1) { return "{" + list[0] + "}"; }

    String[] initial = list.GetRange(0, list.Count - 1).ToArray();
    return "{" + String.Join(", ", initial) + " and " + list[list.Count - 1] + "}";
}

If I was maintaining the code, I’d prefer this to more clever versions.

Leave a Comment