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

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » Collections Data Structure » IComparableScreenshots 
Returns the index of the first occurrence in a sequence by using the default equality comparer.
  
//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;
        }
        /// <summary>  Returns the index of the first occurrence in a sequence by using the default equality comparer. 
        ///</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> 
        /// <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)
            where TSource : IEquatable<TSource>
        {
            return list.Ext_IndexOf<TSource>(value, EqualityComparer<TSource>.Default);
        }
    }
}

   
    
  
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 a specified IEqualityComparer.
3.IComparable Interface defines a type-specific comparison
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.