Almost always, the first extensions methods I add to a system are these:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public static class EnumerableExtensions { public static string JoinToString<T>(this IEnumerable<T> enumerable, Func<T, object> selector, string separator) { return enumerable.Select(selector).JoinToString(separator); } public static string JoinToString<T>(this IEnumerable<T> enumerable, Func<T, object> selector) { return enumerable.Select(selector).JoinToString(", "); } public static string JoinToString(this IEnumerable enumerable, string separator) { var strings = enumerable.Cast<object>() .Select(e => e == null ? "(null)" : e.ToString()) .ToArray(); return string.Join(separator, strings); } } |
which allows me to format any sequence of stuff sensibly, easily, and inline, e.g. like so:
1 |
Log.WarnFormat("Some stuff happened to stuff with these names: {0}", names.JoinToString(", ")); |
and so:
1 |
Log.WarnFormat("Some stuff happened to things with these names: {0}", things.JoinToString(t => t.Name)); |
and so:
1 2 3 4 5 6 7 |
catch(ReflectionTypeLoadException exception) { Log.ErrorFormat(@"ZOMG!!111something happened!! {0}", exception.LoaderExceptions.JoinToString(Environment.NewLine)); } |
Nifty!