Adding C# solution.
[IEnumerableExtras.git] / C# / ListIndexOf.cs
blobf7887ae20d8fe729d488a2ad71c874cb53aa1dc9
1 using System;
2 using System.Collections.Generic;
4 namespace IEnumerableExtras
6 public static class ListIndexOf
8 /// <summary>
9 /// Return index of first element of this list satisfying filter function or -1.
10 /// </summary>
11 /// <typeparam name="T"></typeparam>
12 /// <param name="list"></param>
13 /// <param name="filterFunc"></param>
14 /// <returns></returns>
15 public static int IndexOf< T >( this IList<T> list, Func<T, bool> filterFunc )
17 return IndexOf( list, filterFunc, 1 );
20 /// <summary>
21 /// Return index of first element of this list satisfying filter function or -1.
22 /// </summary>
23 /// <typeparam name="T"></typeparam>
24 /// <param name="list"></param>
25 /// <param name="quant">Quntifier, index will be divisible by this number</param>
26 /// <param name="filterFunc"></param>
27 /// <returns></returns>
28 public static int IndexOf< T >( this IList<T> list, Func<T, bool> filterFunc, int quant )
30 if ( quant <= 0 )
32 throw new ArgumentException( "Qunatifier must be greater than zero.", "quant" );
35 int index = -1;
36 for ( int i = 0; i < list.Count; i += quant )
38 if ( filterFunc( list[ i ] ) )
40 index = i;
41 break;
45 return index;