This method is used to search for an element which matches the conditions defined by a specified predicate and returns the zero-based index of the last occurrence within the List<T> or a portion of it. There are 3 methods in the overload list of this method:
- FindLastIndex(Predicate<T>) Method
- FindLastIndex(Int32, Predicate<T>) Method
- FindLastIndex(Int32, Int32, Predicate<T>) Method
Here, we will discuss only the first method i.e. FindLastIndex(Predicate<T>)
List<T>.FindLastIndex(Predicate<T>) Method 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<T>.
public int FindLastIndex (Predicate <T> match);
Here, the match is the Predicate <T> delegate that defines the conditions of the element to search for.
Return Value: If the element is found then it returns the zero-based index of type int or Int32 of the last element that matches a specified condition by the parameter “match”. And if not found then it returns “-1”.
Exception: This method will throw ArgumentNullException if the
match is null.
Example 1: In this example, creating a List named “PC” that contains some element. Our task is to find an element named “Computer” and prints its index.
Example 2: This example is the extended form of the previous example. In this example, we use an XML file and search an item and prints the index of that item. If the item is not found then prints “-1” and if found then prints the index. The item is\ “GeeksForGeeks”.
- The List<T> is searched backward starting at the last element and ending at the first element.
- The Predicate<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<T> are individually passed to the Predicate<T> delegate.
- This method performs a linear search; therefore, this method is an O(n) operation, where n is Count.
- List FindLastIndex() Method in C# | Set -2
- How to sort a list in C# | List.Sort() Method Set -1
- How to sort a list in C# | List.Sort() Method Set -2
- How to set the Height of the Drop-Down List in the ComboBox in C#?
- How to set the Width of the Drop-Down List in the ComboBox in C#?
- Reverse a singly Linked List in groups of given size | Set 3
- How to set vertical space between the list of items using CSS ?
- C# | List.TrimExcess Method
- List BinarySearch() Method in C#
- List.FindIndex() Method in C# with Examples
- Array.GetValue() Method in C# with Examples | Set - 1
- C# | Graphics.DrawLine() Method | Set - 1
- How to use Array.BinarySearch() Method in C# | Set -1
- C# | String.IndexOf( ) Method | Set - 1
- C# | Join() Method | Set - 1
- C# | Join Method | Set - 2
- C# | Math.Round() Method | Set - 1
- C# | Math.Round() Method | Set – 2
- C# | Math.Abs() Method | Set - 1
- C# | Math.Abs() Method | Set - 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.