2 using System
.Collections
.Generic
;
5 public static class IEnumerableRocks
8 public static string Implode
<TSource
, TResult
> (this IEnumerable
<TSource
> self
, string separator
, Func
<TSource
, TResult
> selector
)
10 return Implode (self
, separator
, (b
, e
) => { b.Append (selector (e).ToString ()); }
);
13 public static string Implode
<TSource
> (this IEnumerable
<TSource
> self
, string separator
, Action
<StringBuilder
, TSource
> appender
)
15 var coll
= self
as ICollection
<TSource
>;
16 if (coll
!= null && coll
.Count
== 0)
20 var s
= new StringBuilder ();
22 foreach (var element
in self
) {
23 if (needSep
&& separator
!= null)
26 appender (s
, element
);
36 public static void Main ()
38 Console
.WriteLine (new [] { "foo", "bar" }
.Implode (", ", e
=> "'" + e
+ "'"));