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.
Note: |
|---|
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".
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
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
*/
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
*/