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 List<(Of <(T>)>).
Namespace:
System.Collections.Generic
Assembly:
mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
Public Function FindLastIndex ( _
match As Predicate(Of T) _
) As Integer
Dim instance As List
Dim match As Predicate(Of T)
Dim returnValue As Integer
returnValue = instance.FindLastIndex(match)
public int FindLastIndex(
Predicate<T> match
)
public:
int FindLastIndex(
Predicate<T>^ match
)
public function FindLastIndex(
match : Predicate<T>
) : int
Return Value
Type:
System..::.Int32The zero-based index of the last occurrence of an element that matches the conditions defined by match, if found; otherwise, –1.
| Exception | Condition |
|---|
| ArgumentNullException |
match is nullNothingnullptra null reference (Nothing in Visual Basic). |
The List<(Of <(T>)>) is searched backward starting at the last element and ending at the first 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 FindLastIndex 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 FindLastIndex(Predicate<(Of <(T>)>)) method overload traverses the list backward from the end, passing each element in turn to the EndsWithSaurus method. The search stops when the EndsWithSaurus method returns true for the element at position 5.
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 FindLastIndex(Int32, Predicate<(Of <(T>)>)) method overload is used to search the list beginning at position 4 and continuing backward to the beginning of the list. It finds the element at position 1. Finally, the FindLastIndex(Int32, Int32, Predicate<(Of <(T>)>)) is used to search the range of three elements list beginning at position 4 and working backward (that is, elements 4, 3, and 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 & _
"FindLastIndex(AddressOf EndsWithSaurus): {0}", _
dinosaurs.FindLastIndex(AddressOf EndsWithSaurus))
Console.WriteLine(vbLf & _
"FindLastIndex(4, AddressOf EndsWithSaurus): {0}", _
dinosaurs.FindLastIndex(4, AddressOf EndsWithSaurus))
Console.WriteLine(vbLf & _
"FindLastIndex(4, 3, AddressOf EndsWithSaurus): {0}", _
dinosaurs.FindLastIndex(4, 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
'
'FindLastIndex(AddressOf EndsWithSaurus): 5
'
'FindLastIndex(4, AddressOf EndsWithSaurus): 1
'
'FindLastIndex(4, 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("\nFindLastIndex(EndsWithSaurus): {0}",
dinosaurs.FindLastIndex(EndsWithSaurus));
Console.WriteLine("\nFindLastIndex(4, EndsWithSaurus): {0}",
dinosaurs.FindLastIndex(4, EndsWithSaurus));
Console.WriteLine("\nFindLastIndex(4, 3, EndsWithSaurus): {0}",
dinosaurs.FindLastIndex(4, 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
FindLastIndex(EndsWithSaurus): 5
FindLastIndex(4, EndsWithSaurus): 1
FindLastIndex(4, 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("\nFindLastIndex(EndsWithSaurus): {0}",
dinosaurs->FindLastIndex(gcnew Predicate<String^>(EndsWithSaurus)));
Console::WriteLine("\nFindLastIndex(4, EndsWithSaurus): {0}",
dinosaurs->FindLastIndex(4, gcnew Predicate<String^>(EndsWithSaurus)));
Console::WriteLine("\nFindLastIndex(4, 3, EndsWithSaurus): {0}",
dinosaurs->FindLastIndex(4, 3, gcnew Predicate<String^>(EndsWithSaurus)));
}
/* This code example produces the following output:
Compsognathus
Amargasaurus
Oviraptor
Velociraptor
Deinonychus
Dilophosaurus
Gallimimus
Triceratops
FindLastIndex(EndsWithSaurus): 5
FindLastIndex(4, EndsWithSaurus): 1
FindLastIndex(4, 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
Reference