Indexes Of element in IEnumerable - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IEnumerable

Description

Indexes Of element in IEnumerable

Demo Code


using System.Linq;
using System.Collections.Generic;
using System;/*from  w  w  w  .java 2 s.  com*/

public class Main{
        public static IEnumerable<int> IndexesOf<T>(this IEnumerable<T> sequence, T item) {
            if (sequence == null) throw new ArgumentNullException("sequence");
            var i = 0;
            foreach (var e in sequence) {
                if (Equals(e, item)) yield return i;
                i++;
            }
        }
}

Related Tutorials