Advertisement
Shimmy

Untitled

Aug 31st, 2017
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4.  
  5. using System.Collections;
  6. using System.Collections.Generic;
  7.  
  8. namespace System.Linq
  9. {
  10. public static partial class Enumerable
  11. {
  12. public static bool Any<TSource>(this IEnumerable<TSource> source)
  13. {
  14. if (source == null)
  15. {
  16. throw Error.ArgumentNull(nameof(source));
  17. }
  18.  
  19. if (source is ICollection<TSource> collectionoft)
  20. {
  21. return collectionoft.Count > 0;
  22. }
  23.  
  24. if (source is IIListProvider<TSource> listProv)
  25. {
  26. return listProv.GetCount(onlyIfCheap: true) > 0;
  27. }
  28.  
  29. if (source is ICollection collection)
  30. {
  31. return collection.Count > 0;
  32. }
  33.  
  34. using (IEnumerator<TSource> e = source.GetEnumerator())
  35. {
  36. return e.MoveNext();
  37. }
  38. }
  39.  
  40. public static bool Any<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
  41. {
  42. if (source == null)
  43. {
  44. throw Error.ArgumentNull(nameof(source));
  45. }
  46.  
  47. if (predicate == null)
  48. {
  49. throw Error.ArgumentNull(nameof(predicate));
  50. }
  51.  
  52. if (source is IIListProvider<TSource> listProv && listProv.GetCount(onlyIfCheap: true) == 0)
  53. {
  54. return false;
  55. }
  56.  
  57. if (source is ICollection collection && collection.Count == 0)
  58. {
  59. return false;
  60. }
  61.  
  62. foreach (TSource element in source)
  63. {
  64. if (predicate(element))
  65. {
  66. return true;
  67. }
  68. }
  69.  
  70. return false;
  71. }
  72.  
  73. public static bool All<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
  74. {
  75. if (source == null)
  76. {
  77. throw Error.ArgumentNull(nameof(source));
  78. }
  79.  
  80. if (predicate == null)
  81. {
  82. throw Error.ArgumentNull(nameof(predicate));
  83. }
  84.  
  85. foreach (TSource element in source)
  86. {
  87. if (!predicate(element))
  88. {
  89. return false;
  90. }
  91. }
  92.  
  93. return true;
  94. }
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement