New test.
[mono.git] / mcs / tests / gtest-anon-33.cs
1 using System;\r
2 using System.Collections.Generic;\r
3 using System.Text;\r
4 \r
5 public static class IEnumerableRocks\r
6 {\r
7 \r
8         public static string Implode<TSource, TResult> (this IEnumerable<TSource> self, string separator, Func<TSource, TResult> selector)\r
9         {\r
10                 return Implode (self, separator, (b, e) => { b.Append (selector (e).ToString ()); });\r
11         }\r
12 \r
13         public static string Implode<TSource> (this IEnumerable<TSource> self, string separator, Action<StringBuilder, TSource> appender)\r
14         {\r
15                 var coll = self as ICollection<TSource>;\r
16                 if (coll != null && coll.Count == 0)\r
17                         return string.Empty;\r
18 \r
19                 bool needSep = false;\r
20                 var s = new StringBuilder ();\r
21 \r
22                 foreach (var element in self) {\r
23                         if (needSep && separator != null)\r
24                                 s.Append (separator);\r
25 \r
26                         appender (s, element);\r
27                         needSep = true;\r
28                 }\r
29 \r
30                 return s.ToString ();\r
31         }\r
32 }\r
33 \r
34 class Test\r
35 {\r
36         public static void Main ()\r
37         {\r
38                 Console.WriteLine (new [] { "foo", "bar" }.Implode (", ", e => "'" + e + "'"));\r
39         }\r
40 }\r