Returns the index of the first occurrence in a sequence by using a specified IEqualityComparer. : IComparable « Collections Data Structure « C# / C Sharp






Returns the index of the first occurrence in a sequence by using a specified IEqualityComparer.

  
//The MIT License (MIT)
//http://arolibraries.codeplex.com/license

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;

namespace AroLibraries.ExtensionMethods.Enumerable
{
    public static class IEnumerableExt
    {

        /// <summary> Returns the index of the first occurrence in a sequence by using aspecified IEqualityComparer. 
        /// </summary> ///
        /// <typeparam name="TSource">The type of the elements of source.</typeparam>
        /// <param name="list">A sequence in which to locate a value.</param> ///
        ///  <param name="value">The object to locate in the sequence</param> /// <param name="comparer">An equality comparer to compare values.</param> 
        /// <returns>The zero-based index of the first occurrence of value within the entire sequence, if found; otherwise, 1.</returns>
        public static int Ext_IndexOf<TSource>(this IEnumerable<TSource> list, TSource value, IEqualityComparer<TSource> comparer)
        {
            int index = 0;
            foreach (var item in list)
            {
                if (comparer.Equals(item, value))
                {
                    return index;
                }
                index++;
            }
            return -1;
        }
   }
}

   
    
  








Related examples in the same category

1.Creates and accesses an array of classes. Implements the IComparable interfaceCreates and accesses an array of classes. Implements the IComparable interface
2.Returns the index of the first occurrence in a sequence by using the default equality comparer.
3.IComparable Interface defines a type-specific comparison