VOOZH about

URL: https://www.geeksforgeeks.org/c-sharp/list-findlastindex-method-in-c-sharp-set-2/

⇱ List FindLastIndex() Method in C# | Set -2 - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

List FindLastIndex() Method in C# | Set -2

Last Updated : 11 Jul, 2025
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: Here, we will discuss only the last two methods.

FindLastIndex(Int32, Predicate<T>) Method

This 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 range of elements in the List<T> that extends from the first element to the specified index.
Syntax: public int FindLastIndex (int start, Predicate<T> match); Parameters: start: It is the starting index from the searching will starts. match: It is the Predicate delegate that defines the conditions of the searched element.
Return Value: If the element is found then it returns the zero-based index of type Int32 of the last occurrence of an element that matches a specified condition by the parameter “match”. And if not found then it returns “-1”. Exceptions:
  • ArgumentNullException: If the match is null.
  • ArgumentOutOfRangeException: If the startis outside the range of valid indexes for the List<T>.
Below programs illustrate the use of above-discussed method: Example 1: Output:
0
Example 2: Output:
2
Example 3: In this example we use an XML file and search an item from a starting index 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”. But here we don’t have the XML file, and here the compiler gives an exception. Runtime Error:
Unhandled Exception: System.ArgumentOutOfRangeException: ArgumentOutOfRange_Index Parameter name: startIndex

FindLastIndex(Int32, Int32, Predicate<T>) Method

This 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 and the list contains the specified number of elements and ends at the specified index.
Syntax: public int FindLastIndex (int startIndex, int count, Predicate<T> match); Parameters: startIndex: It is the zero-based starting index of the backward search. count: It is the number of elements in the section to search. match: It 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 Int32 of the last element that matches a specified condition by the parameter “match”. And if not found then it returns “-1”. Exceptions:
  • ArgumentNullException: if the “match” is null.
  • ArgumentOutOfRangeException: if “startIndex” is outside the range or “count” is less than 0(Zero) or “startIndex” and “count” do not specify a valid section in the List
Example:
Output:
The index of Java is: 2
Note:
  • The List<T> is searched backward starting at startIndex 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 the number of elements from the beginning of the List<T> to start.
Comment

Explore