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>
?
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With