/Test/Mono.Cecil.Tests/Linq.cs

http://github.com/jbevain/cecil · C# · 47 lines · 36 code · 11 blank · 0 comment · 3 complexity · 81d2c5ace0808103a747a8da34981915 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using Mono;
  4. #if !NET_3_5 && !NET_4_0
  5. namespace System.Linq {
  6. static class Enumerable {
  7. public static IEnumerable<TRet> Select<TItem, TRet> (this IEnumerable<TItem> self, Func<TItem, TRet> selector)
  8. {
  9. foreach (var item in self)
  10. yield return selector (item);
  11. }
  12. public static IEnumerable<T> Where<T> (this IEnumerable<T> self, Func<T, bool> predicate)
  13. {
  14. foreach (var item in self)
  15. if (predicate (item))
  16. yield return item;
  17. }
  18. public static List<T> ToList<T> (this IEnumerable<T> self)
  19. {
  20. return new List<T> (self);
  21. }
  22. public static T [] ToArray<T> (this IEnumerable<T> self)
  23. {
  24. return self.ToList ().ToArray ();
  25. }
  26. public static T First<T> (this IEnumerable<T> self)
  27. {
  28. using (var enumerator = self.GetEnumerator ()) {
  29. if (!enumerator.MoveNext ())
  30. throw new InvalidOperationException ();
  31. return enumerator.Current;
  32. }
  33. }
  34. }
  35. }
  36. #endif