SuperLinq SuperLinq
SuperLinq SuperLinq
DocFX + Singulink = ♥

Search Results for

    Method FindLastIndex

    | Edit this page

    FindLastIndex<TSource>(IEnumerable<TSource>, Func<TSource, bool>)

    Searches for an element that matches the conditions defined by the specified predicate and returns the zero-based index of the last occurrence within the entire IEnumerable<T>.

    Declaration
    public static int FindLastIndex<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
    Parameters
    Type Name Description
    IEnumerable<TSource> source

    The source sequence.

    Func<TSource, bool> predicate

    The predicate that defines the conditions of the element to search for.

    Returns
    Type Description
    int

    The zero-based index of the last occurrence of an element that matches the conditions defined by predicate within the entire IEnumerable<T>, if found; otherwise, -1.

    Type Parameters
    Name Description
    TSource

    The type of elements of source

    Remarks

    The IEnumerable<T> is searched forward starting at the first element and ending at the last element, and the index of the last instance of an element that matches the conditions defined by predicate is returned.

    The predicate is a delegate to a method that returns true if the object passed to it matches the conditions defined in the delegate. The elements of the current IEnumerable<T> are individually passed to the predicate delegate.

    This operator executes immediately.

    Examples

    The following code example demonstrates how to find the index of the last element that satisfies a condition, using FindLastIndex.

    var sequence = new[]
    {
        (key: 2, name: "Frank"),
        (key: 3, name: "Jill"),
        (key: 5, name: "Dave"),
        (key: 8, name: "Jack"),
        (key: 12, name: "Judith"),
        (key: 14, name: "Robert"),
        (key: 1, name: "Adam"),
    };
                
    // Find the first index that matches a condition
    Console.WriteLine(
        "'J' starts at index {0}",
        sequence.FindLastIndex(
            x => x.name.StartsWith("J")));
    
    Console.WriteLine(
        "'Ji' starts at index {0}",
        sequence.FindLastIndex(
            x => x.name.StartsWith("Ji")));
    
    Console.WriteLine(
        "'K' starts at index {0}",
        sequence.FindLastIndex(
            x => x.name.StartsWith("K")));
    
    // This code produces the following output:
    // 'J' starts at index 4
    // 'Ji' starts at index 1
    // 'K' starts at index -1
    
    Exceptions
    Type Condition
    ArgumentNullException

    source or predicate is null.

    | Edit this page

    FindLastIndex<TSource>(IEnumerable<TSource>, Func<TSource, bool>, Index)

    Searches for an element that matches the conditions defined by the specified predicate and returns the zero-based index of the last occurrence within the range of elements in the IEnumerable<T> that extends backwards from the specified index to the first element.

    Declaration
    public static int FindLastIndex<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate, Index index)
    Parameters
    Type Name Description
    IEnumerable<TSource> source

    The source sequence.

    Func<TSource, bool> predicate

    The predicate that defines the conditions of the element to search for.

    Index index

    The Index of the ending element within the sequence.

    Returns
    Type Description
    int

    The zero-based index of the last occurrence of an element that matches the conditions defined by predicate within the the range of elements in the IEnumerable<T> that extends backwards from index to the first element, if found; otherwise, -1.

    Type Parameters
    Name Description
    TSource

    The type of elements of source

    Remarks

    The IEnumerable<T> is searched forward starting at the first element and ending at index, and the index of the last instance of an element that matches the conditions defined by predicate is returned.

    The predicate is a delegate to a method that returns true if the object passed to it matches the conditions defined in the delegate. The elements of the current IEnumerable<T> are individually passed to the predicate delegate.

    This operator executes immediately.

    Examples

    The following code example demonstrates how to find the index of the last element that satisfies a condition, using FindLastIndex.

    var sequence = new[]
    {
        (key: 2, name: "Frank"),
        (key: 3, name: "Jill"),
        (key: 5, name: "Dave"),
        (key: 8, name: "Jack"),
        (key: 12, name: "Judith"),
        (key: 14, name: "Robert"),
        (key: 1, name: "Adam"),
    };
                
    // Find the first index that matches a condition
    Console.WriteLine(
        "'J' starts at index {0}, before index 0",
        sequence.FindLastIndex(
            x => x.name.StartsWith("J"),
            0));
    
    Console.WriteLine(
        "'J' starts at index {0}, before index ^2",
        sequence.FindLastIndex(
            x => x.name.StartsWith("J"),
            ^2));
    
    // This code produces the following output:
    // 'J' starts at index -1, before index 0
    // 'J' starts at index 4, before index ^2
    
    Exceptions
    Type Condition
    ArgumentNullException

    source or predicate is null.

    | Edit this page

    FindLastIndex<TSource>(IEnumerable<TSource>, Func<TSource, bool>, Index, int)

    Searches for an element that matches the conditions defined by the specified predicate and returns the zero-based index of the last occurrence within the range of elements in the IEnumerable<T> that ends at the specified index to the last element and contains the specified number of elements.

    Declaration
    public static int FindLastIndex<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate, Index index, int count)
    Parameters
    Type Name Description
    IEnumerable<TSource> source

    The source sequence.

    Func<TSource, bool> predicate

    The predicate that defines the conditions of the element to search for.

    Index index

    The Index of the ending element within the sequence.

    int count

    The number of elements in the section to search.

    Returns
    Type Description
    int

    The zero-based index of the last occurrence of an element that matches the conditions defined by predicate within the the range of elements in the IEnumerable<T> that that ends at index and contains count number of elements, if found; otherwise, -1.

    Type Parameters
    Name Description
    TSource

    The type of elements of source

    Remarks

    The IEnumerable<T> is searched forward starting at the first element and ending at index, and the index of the last instance of an element that matches the conditions defined by predicate no earlier in the sequence than count items before index is returned.

    The predicate is a delegate to a method that returns true if the object passed to it matches the conditions defined in the delegate. The elements of the current IEnumerable<T> are individually passed to the predicate delegate.

    This operator executes immediately.

    Examples

    The following code example demonstrates how to find the index of the last element that satisfies a condition, using FindLastIndex.

    var sequence = new[]
    {
        (key: 2, name: "Frank"),
        (key: 3, name: "Jill"),
        (key: 5, name: "Dave"),
        (key: 8, name: "Jack"),
        (key: 12, name: "Judith"),
        (key: 14, name: "Robert"),
        (key: 1, name: "Adam"),
    };
    
    // Find the first index that matches a condition
    Console.WriteLine(
        "'J' starts at index {0}, before index 0",
        sequence.FindLastIndex(
            x => x.name.StartsWith("J"),
            1,
            1));
    
    Console.WriteLine(
        "'J' starts at index {0}, before index ^2",
        sequence.FindLastIndex(
            x => x.name.StartsWith("J"),
            ^2));
    
    // This code produces the following output:
    // 'J' starts at index -1, before index 0
    // 'J' starts at index 4, before index ^2
    
    Exceptions
    Type Condition
    ArgumentNullException

    source or predicate is null.

    ArgumentOutOfRangeException

    count is less than 0.

    © SuperLinq Authors. All rights reserved.