IndexOf

The IList<T> interface has an IndexOf method (and a corresponding Item property). The IEnumerable<T> interface does not have an equivalent IndexOf method (though it does have an extension method ElementAt<T> that mirrors the Item property).

Naturally, you can add your own extension method:

public static int IndexOf<TSource>(this IEnumerable<TSource> source, TSource t)
{
    var index = source.TakeWhile(s => s != t).Count();
    return index == source.Count() ? -1 : index;
}
Post a comment or leave a trackback: Trackback URL.

Leave a comment