Find Index in IEnumerable by Predicate - CSharp System.Collections

CSharp examples for System.Collections:IEnumerable

Description

Find Index in IEnumerable by Predicate

Demo Code


using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;//from  ww  w .  j av a2 s.c o m

public class Main{
        public static int FindIndex<T>(this IEnumerable<T> source, Predicate<T> predicate)
        {
            int index = 0;
            foreach (T elem in source)
            {
                if (predicate(elem))
                {
                    return index;
                }
                ++index;
            }

            return -1;
        }
}

Related Tutorials