static class SBExtention
{
static string AppendCollection<T>(this StringBuilder sb,
IEnumerable<T> coll,
Func<T,string> action)
{
foreach(T t in coll)
{
sb.Append(action(t));
sb.Append("\n");
}
return sb.ToString();
}
}
However, I think you would be better off returning a StringBuilder. So you can link it:
static StringBuilder AppendCollection<T>(this StringBuilder sb,
IEnumerable<T> coll,
Func<T,string> action)
{
return sb;
}
string peopleAndOrders = sb.AppendCollection (people, p => p.ToString ()) .AppendCollection (orders, o => o.ToString ()). ToString ();
And I agree with Jennifer about the default case:
public static StringBuilder AppendCollection<TItem>(
this StringBuilder builder,
IEnumerable<TItem> items)
{
return AppendCollection(builder, items, x=>x.ToString());
}
string peopleAndOrders = sb.AppendCollection (people) .AppendCollection (orders) .ToString ();
source
share