List FindLastIndex() Method in C# | Set -2
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 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:
// C# program to illustrate the // List<T>.FindLastIndex(Int32, // Predicate <T>) Method using System; using System.Collections.Generic; class GFG { // Main Method public static void Main() { // List creation // List name is "PC" List< string > PC = new List< string >(); // elements in the List PC.Add( "Computer" ); PC.Add( "keyboard" ); PC.Add( "laptop" ); PC.Add( "mouse" ); // the search will starts from index 2 int indx = PC.FindLastIndex(2, FindIndex); Console.WriteLine(indx); } // Conditional method private static bool FindIndex( string g) { if (g == "Computer" ) { return true ; } else { return false ; } } } |
Output:
0
Example 2:
// C# program to illustrate the // List<T>.FindLastIndex(Int32, // Predicate <T>) Method using System; using System.Collections.Generic; class GFG { // Main Method public static void Main() { // List creation // List name is "PC" List< int > PC = new List< int >(); // elements in the List PC.Add(3); PC.Add(4); PC.Add(5); PC.Add(6); // condition is "FindIndex" int indx = PC.FindLastIndex(2, FindIndex); Console.WriteLine(indx); } // Conditional method private static bool FindIndex( int g) { // search for "5" if (g == 5) { return true ; } else { return false ; } } } |
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.
// C# program to illustrate the // List<T>.FindLastIndex(Int32, // Predicate <T>) Method using System; using System.Collections.Generic; using System.Linq; class GFG { // here List<T> contains the object "gfg" using // data from a sample XML file // List initialize private static List<gfg> geeks = new List<gfg>(); public static void Main() { // if the item is found then // it prints the index // if not found prints "-1" int x = geeks.FindLastIndex(3, FindGFG); Console.WriteLine(x); } // conditional method private static bool FindGFG(gfg g) { // item is "GeeksForGeeks" if (g.G == "GeeksForGeeks" ) { return true ; } else { return false ; } } } public class gfg { public string G { get ; set ; } } |
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:
// C# Program to illustrate the // FindLastIndex(Int32, Int32, // Predicate<T>) Method using System; using System.Collections.Generic; class GFG { public static void Main() { // List name is "mylist" List< string > mylist = new List< string >(); // Elements in the List mylist.Add( "C" ); mylist.Add( "C++" ); mylist.Add( "Java" ); mylist.Add( "Python" ); mylist.Add( "C#" ); mylist.Add( "HTML" ); mylist.Add( "Java" ); mylist.Add( "PHP" ); // the search will starts from index 2 // the number of element is 3 int indx = mylist.FindLastIndex(2, 3, FindIndex); Console.WriteLine( "The index of Java is: " +indx); } // Conditional method private static bool FindIndex( string g) { if (g == "Java" ) { return true ; } else { return false ; } } } |
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.