Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Search for an element matching the conditions and return the zero-based index of the last occurrence within the entire List in C#
The FindLastIndex() method in C# searches for an element that matches the conditions defined by a specified predicate and returns the zero-based index of the last occurrence within the entire List<T>. This method is particularly useful when you need to find the rightmost element that satisfies certain criteria.
Syntax
Following is the syntax for the FindLastIndex() method −
public int FindLastIndex(Predicate<T> match)
Parameters
-
match − The
Predicate<T>delegate that defines the conditions of the element to search for.
Return Value
Returns the zero-based index of the last occurrence of an element that matches the conditions. If no element is found, it returns -1.
Using FindLastIndex() with Divisibility Condition
This example demonstrates finding the last index of an element divisible by 10 −
using System;
using System.Collections.Generic;
public class Demo {
private static bool IsMultipleOfTen(int i) {
return ((i % 10) == 0);
}
public static void Main(String[] args) {
List<int> list = new List<int>();
list.Add(200);
list.Add(215);
list.Add(310);
list.Add(500);
list.Add(600);
Console.WriteLine("List elements...");
foreach (int i in list) {
Console.WriteLine(i);
}
Console.WriteLine("Last Index of the satisfying condition = " + list.FindLastIndex(IsMultipleOfTen));
}
}
The output of the above code is −
List elements... 200 215 310 500 600 Last Index of the satisfying condition = 4
Using FindLastIndex() with Even Number Condition
This example finds the last index of an even number in the list −
using System;
using System.Collections.Generic;
public class Demo {
private static bool IsEven(int i) {
return ((i % 2) == 0);
}
public static void Main(String[] args) {
List<int> list = new List<int>();
list.Add(200);
list.Add(215);
list.Add(310);
list.Add(500);
list.Add(655);
Console.WriteLine("List elements...");
foreach (int i in list) {
Console.WriteLine(i);
}
Console.WriteLine("Last Index of the satisfying condition = " + list.FindLastIndex(IsEven));
}
}
The output of the above code is −
List elements... 200 215 310 500 655 Last Index of the satisfying condition = 3
Using Lambda Expression with FindLastIndex()
You can also use lambda expressions for more concise code −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(String[] args) {
List<int> numbers = new List<int> { 15, 30, 45, 60, 75, 90, 105 };
Console.WriteLine("List elements...");
foreach (int num in numbers) {
Console.WriteLine(num);
}
int lastEvenIndex = numbers.FindLastIndex(x => x % 2 == 0);
int lastDivisibleBy15Index = numbers.FindLastIndex(x => x % 15 == 0);
Console.WriteLine("Last even number index: " + lastEvenIndex);
Console.WriteLine("Last number divisible by 15 index: " + lastDivisibleBy15Index);
}
}
The output of the above code is −
List elements... 15 30 45 60 75 90 105 Last even number index: 5 Last number divisible by 15 index: 6
Conclusion
The FindLastIndex() method provides an efficient way to locate the last occurrence of an element matching specific conditions in a List<T>. It returns -1 when no matching element is found, making it essential to handle this case in production code.
