Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IList<T>.FindIndex(Int32, Predicate <T>)

Tags:

c#

.net-3.5

There is a List<T>.FindIndex(Int32, Predicate <T>). That method is exactly what I want to for a IList<T> object.
I know IList has a method IndexOf(T) but I need the predicate to define the comparing algorithm.

Is there a method, extension method, LINQ or some code to find the index of an item in a IList<T>?

like image 627
thersch Avatar asked Dec 07 '12 16:12

thersch


1 Answers

Well you can really easily write your own extension method:

public static int FindIndex<T>(this IList<T> source, int startIndex,
                               Predicate<T> match)
{
    // TODO: Validation
    for (int i = startIndex; i < source.Count; i++)
    {
        if (match(source[i]))
        {
            return i;
        }
    }
    return -1;
}
like image 86
Jon Skeet Avatar answered Nov 05 '22 17:11

Jon Skeet