Adding C# solution.
[IEnumerableExtras.git] / C# / EnumerableSome.cs
blob79f614b0be570951a8ea40b82a63eea2b6aa94cc
1 using System;
2 using System.Collections.Generic;
4 namespace IEnumerableExtras
6 /// <summary>
7 /// Provides <see cref="IEnumerable{T}"/> Some/All extension.
8 /// </summary>
9 public static class EnumerableSome
11 /// <summary>
12 /// Check if any items in this collection satisfy <paramref name="testFunc"/>.
13 /// </summary>
14 /// <typeparam name="T"></typeparam>
15 /// <param name="collection"></param>
16 /// <param name="testFunc"></param>
17 /// <returns></returns>
18 public static bool Some< T >( this IEnumerable<T> collection, Func<T, bool> testFunc )
20 foreach ( T enumerable in collection )
22 if ( testFunc( enumerable ) )
24 return true;
28 return false;
31 /// <summary>
32 /// Check if all of this collection items satisfy <paramref name="testFunc"/>.
33 /// </summary>
34 /// <typeparam name="T"></typeparam>
35 /// <param name="collection"></param>
36 /// <param name="testFunc"></param>
37 /// <returns></returns>
38 public static bool All< T >( this IEnumerable<T> collection, Func<T, bool> testFunc )
40 foreach ( T enumerable in collection )
42 if ( !testFunc( enumerable ) )
44 return false;
48 return true;