FindIndex Method (Predicate(T))
.NET Framework Class Library
List<(Of <(T>)>)..::.FindIndex Method (Predicate<(Of <(T>)>))

Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the entire List<(Of <(T>)>).

Namespace:  System.Collections.Generic
Assembly:  mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
Public Function FindIndex ( _
    match As Predicate(Of T) _
) As Integer
Visual Basic (Usage)
Dim instance As List
Dim match As Predicate(Of T)
Dim returnValue As Integer

returnValue = instance.FindIndex(match)
C#
public int FindIndex(
    Predicate<T> match
)
Visual C++
public:
int FindIndex(
    Predicate<T>^ match
)
JScript
public function FindIndex(
    match : Predicate<T>
) : int

Parameters

match
Type: System..::.Predicate<(Of <(T>)>)
The Predicate<(Of <(T>)>) delegate that defines the conditions of the element to search for.

Return Value

Type: System..::.Int32
The zero-based index of the first occurrence of an element that matches the conditions defined by match, if found; otherwise, –1.
ExceptionCondition
ArgumentNullException

match is nullNothingnullptra null reference (Nothing in Visual Basic).

The List<(Of <(T>)>) is searched forward starting at the first element and ending at the last element.

The Predicate<(Of <(T>)>) 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 List<(Of <(T>)>) are individually passed to the Predicate<(Of <(T>)>) delegate.

This method performs a linear search; therefore, this method is an O(n) operation, where n is Count.

The following code example demonstrates all three overloads of the FindIndex method. A List<(Of <(T>)>) of strings is created, containing 8 dinosaur names, two of which (at positions 1 and 5) end with "saurus". The code example also defines a search predicate method named EndsWithSaurus, which accepts a string parameter and returns a Boolean value indicating whether the input string ends in "saurus".

The FindIndex(Predicate<(Of <(T>)>)) method overload traverses the list from the beginning, passing each element in turn to the EndsWithSaurus method. The search stops when the EndsWithSaurus method returns true for the element at position 1.

NoteNote:

In C# and Visual Basic, it is not necessary to create the Predicate<string> delegate (Predicate(Of String) in Visual Basic) explicitly. These languages infer the correct delegate from context and create it automatically.

The FindIndex(Int32, Predicate<(Of <(T>)>)) method overload is used to search the list beginning at position 2 and continuing to the end of the list. It finds the element at position 5. Finally, the FindIndex(Int32, Int32, Predicate<(Of <(T>)>)) overload is used to search the range of three elements beginning at position 2. It returns –1 because there are no dinosaur names in that range that end with "saurus".

Visual Basic
Imports System
Imports System.Collections.Generic

Public Class Example

    Public Shared Sub Main()

        Dim dinosaurs As New List(Of String)

        dinosaurs.Add("Compsognathus")
        dinosaurs.Add("Amargasaurus")
        dinosaurs.Add("Oviraptor")
        dinosaurs.Add("Velociraptor")
        dinosaurs.Add("Deinonychus")
        dinosaurs.Add("Dilophosaurus")
        dinosaurs.Add("Gallimimus")
        dinosaurs.Add("Triceratops")

        Console.WriteLine()
        For Each dinosaur As String In dinosaurs
            Console.WriteLine(dinosaur)
        Next

        Console.WriteLine(vbLf & _
            "FindIndex(AddressOf EndsWithSaurus): {0}", _
            dinosaurs.FindIndex(AddressOf EndsWithSaurus))

        Console.WriteLine(vbLf & _
            "FindIndex(2, AddressOf EndsWithSaurus): {0}", _
            dinosaurs.FindIndex(2, AddressOf EndsWithSaurus))

        Console.WriteLine(vbLf & _
            "FindIndex(2, 3, AddressOf EndsWithSaurus): {0}", _
            dinosaurs.FindIndex(2, 3, AddressOf EndsWithSaurus))

    End Sub

    ' Search predicate returns true if a string ends in "saurus".
    Private Shared Function EndsWithSaurus(ByVal s As String) _
        As Boolean

        ' AndAlso prevents evaluation of the second Boolean
        ' expression if the string is so short that an error
        ' would occur.
        If (s.Length > 5) AndAlso _
            (s.Substring(s.Length - 6).ToLower() = "saurus") Then
            Return True
        Else
            Return False
        End If
    End Function
End Class

' This code example produces the following output:
'
'Compsognathus
'Amargasaurus
'Oviraptor
'Velociraptor
'Deinonychus
'Dilophosaurus
'Gallimimus
'Triceratops
'
'FindIndex(AddressOf EndsWithSaurus): 1
'
'FindIndex(2, AddressOf EndsWithSaurus): 5
'
'FindIndex(2, 3, AddressOf EndsWithSaurus): -1

C#
using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        List<string> dinosaurs = new List<string>();

        dinosaurs.Add("Compsognathus");
        dinosaurs.Add("Amargasaurus");
        dinosaurs.Add("Oviraptor");
        dinosaurs.Add("Velociraptor");
        dinosaurs.Add("Deinonychus");
        dinosaurs.Add("Dilophosaurus");
        dinosaurs.Add("Gallimimus");
        dinosaurs.Add("Triceratops");

        Console.WriteLine();
        foreach(string dinosaur in dinosaurs)
        {
            Console.WriteLine(dinosaur);
        }

        Console.WriteLine("\nFindIndex(EndsWithSaurus): {0}", 
            dinosaurs.FindIndex(EndsWithSaurus));

        Console.WriteLine("\nFindIndex(2, EndsWithSaurus): {0}",
            dinosaurs.FindIndex(2, EndsWithSaurus));

        Console.WriteLine("\nFindIndex(2, 3, EndsWithSaurus): {0}",
            dinosaurs.FindIndex(2, 3, EndsWithSaurus));
    }

    // Search predicate returns true if a string ends in "saurus".
    private static bool EndsWithSaurus(String s)
    {
        if ((s.Length > 5) && 
            (s.Substring(s.Length - 6).ToLower() == "saurus"))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

/* This code example produces the following output:

Compsognathus
Amargasaurus
Oviraptor
Velociraptor
Deinonychus
Dilophosaurus
Gallimimus
Triceratops

FindIndex(EndsWithSaurus): 1

FindIndex(2, EndsWithSaurus): 5

FindIndex(2, 3, EndsWithSaurus): -1
 */

Visual C++
using namespace System;
using namespace System::Collections::Generic;

// Search predicate returns true if a string ends in "saurus".
bool EndsWithSaurus(String^ s)
{
    if ((s->Length > 5) && 
        (s->Substring(s->Length - 6)->ToLower() == "saurus"))
    {
        return true;
    }
    else
    {
        return false;
    }
};

void main()
{
    List<String^>^ dinosaurs = gcnew List<String^>();

    dinosaurs->Add("Compsognathus");
    dinosaurs->Add("Amargasaurus");
    dinosaurs->Add("Oviraptor");
    dinosaurs->Add("Velociraptor");
    dinosaurs->Add("Deinonychus");
    dinosaurs->Add("Dilophosaurus");
    dinosaurs->Add("Gallimimus");
    dinosaurs->Add("Triceratops");

    Console::WriteLine();
    for each(String^ dinosaur in dinosaurs )
    {
        Console::WriteLine(dinosaur);
    }

    Console::WriteLine("\nFindIndex(EndsWithSaurus): {0}", 
        dinosaurs->FindIndex(gcnew Predicate<String^>(EndsWithSaurus)));

    Console::WriteLine("\nFindIndex(2, EndsWithSaurus): {0}",
        dinosaurs->FindIndex(2, gcnew Predicate<String^>(EndsWithSaurus)));

    Console::WriteLine("\nFindIndex(2, 3, EndsWithSaurus): {0}",
        dinosaurs->FindIndex(2, 3, gcnew Predicate<String^>(EndsWithSaurus)));
}

/* This code example produces the following output:

Compsognathus
Amargasaurus
Oviraptor
Velociraptor
Deinonychus
Dilophosaurus
Gallimimus
Triceratops

FindIndex(EndsWithSaurus): 1

FindIndex(2, EndsWithSaurus): 5

FindIndex(2, 3, EndsWithSaurus): -1
 */

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
Page view tracker