using System; using System.Collections.Generic; using System.Text; namespace System.Linq { public static partial class Enumerable { public static IEnumerable Reverse (this IEnumerable source) { if (source == null) throw new ArgumentNullException ("source"); var list = source.ToList (); for (int i = list.Count - 1; i >= 0; i--) yield return list [i]; } public static IEnumerable DefaultIfEmpty (this IEnumerable source) { return source.DefaultIfEmpty (default (TSource)); } public static IEnumerable DefaultIfEmpty (this IEnumerable source, TSource defaultValue) { if (source == null) throw new ArgumentNullException ("source"); bool empty = true; foreach (TSource element in source) { empty = false; yield return element; } if (empty) yield return defaultValue; } // A bonus query operator! It allows you to do this: // myQuery.ForEach (Console.WriteLine); public static void ForEach (this IEnumerable source, Action action) { foreach (TSource element in source) action (element); } } }