This project is read-only.
1
Vote

Duplication between ToString and ToCommaSeparatedList

description

The code in the EnumerableExtensions.ToString and EnumerableExtensions.ToCommaSeparatedList methods is virtually identical. You can simplify this to:
 
public static string ToString<T>(this IEnumerable<T> collection, Func<T, string> toString, string separator)
{
if (null == collection) throw new ArgumentNullException("collection");
if (null == toString) throw new ArgumentNullException("toString");

bool started = false;
var sb = new StringBuilder();
foreach (T item in collection)
{
    if (started) sb.Append(separator);
    sb.Append(toString(item));
    started = true;
}

return sb.ToString();
}
 
public static string ToString<T>(this IEnumerable<T> collection, string separator)
{
return ToString(collection, item => (null == item) ? null : item.ToString(), separator);
}
 
public static string ToString(this IEnumerable<string> collection, string separator)
{
return ToString(collection, item => item, separator);
}
 
public static string ToCommaSeparatedList<T>(this IEnumerable<T> enumerable)
{
return ToString(enumerable, ",");
}
 
public static string ToCommaSeparatedList(this IEnumerable<string> enumerable)
{
return ToString(enumerable, item => item, ",");
}

comments