Gets the index of the given element in this enumeration, or -1 when the item is absent from the enumeration. - CSharp System

CSharp examples for System:Enum

Description

Gets the index of the given element in this enumeration, or -1 when the item is absent from the enumeration.

Demo Code


using System.Text;
using System.Collections.Generic;
using System;/*from   ww  w .ja v  a  2 s  . com*/

public class Main{
        /// <summary>
        /// Gets the index of the given element in this enumeration, or -1 when the item is absent from the enumeration.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="src"></param>
        /// <param name="item"></param>
        /// <returns></returns>
        public static int IndexOf<T>(this IEnumerable<T> src, T item)
        {
            int index = 0;
            foreach (var srcItem in src)
            {
                if (Object.Equals((object)item, (object)srcItem)) return index;
                index++;
            }
            return -1;
        }
}

Related Tutorials