Friday, September 22, 2017

Convert a list to a comma string

It’s nothing special, but I’ve found this useful in the past and probably will again so here for my memory bank is a function that converts a list to a comma string


using System.Collections.Generic;
using System.Text;

namespace Utils
{
  public static class ListHelper
  {
    public static string ToCommaString<T>(this IEnumerable<T> list, string separator = ", ")
    {
      var builder = new StringBuilder();

      foreach (var t in list)
      {
        if (builder.Length > 0)
          builder.Append(separator);
        builder.Append(t);
      }

      return builder.ToString();
    }
  }
}

No comments: