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
How to get all elements of a List that match the conditions specified by the predicate in C#?
To get all elements of a List that match specific conditions, C# provides the FindAll method which accepts a predicate − a delegate that defines the filtering criteria. This method returns a new List containing only the elements that satisfy the condition.
Syntax
Following is the syntax for using FindAll with a predicate −
List<T> result = list.FindAll(predicate);
The predicate can be defined as a method, lambda expression, or anonymous delegate −
// Method predicate
private static bool MethodName(T item) {
return condition;
}
// Lambda expression predicate
list.FindAll(x => condition);
Using FindAll with Method Predicate
Example − Finding Elements Divisible by 3
using System;
using System.Collections.Generic;
public class Demo {
private static bool IsDivisibleByThree(int i) {
return ((i % 3) == 0);
}
public static void Main(String[] args) {
List<int> list = new List<int>();
list.Add(9);
list.Add(15);
list.Add(20);
list.Add(40);
list.Add(50);
list.Add(60);
Console.WriteLine("Original list elements:");
foreach (int i in list) {
Console.WriteLine(i);
}
List<int> result = list.FindAll(IsDivisibleByThree);
Console.WriteLine("\nElements divisible by 3:");
foreach (int i in result) {
Console.WriteLine(i);
}
}
}
The output of the above code is −
Original list elements: 9 15 20 40 50 60 Elements divisible by 3: 9 15 60
Using FindAll with Lambda Expressions
Example − Multiple Filtering Conditions
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(String[] args) {
List<int> numbers = new List<int> { 5, 10, 15, 20, 25, 30, 35, 40 };
Console.WriteLine("Original numbers:");
foreach (int num in numbers) {
Console.Write(num + " ");
}
// Find numbers greater than 20
List<int> greaterThan20 = numbers.FindAll(x => x > 20);
Console.WriteLine("<br>\nNumbers greater than 20:");
foreach (int num in greaterThan20) {
Console.Write(num + " ");
}
// Find even numbers
List<int> evenNumbers = numbers.FindAll(x => x % 2 == 0);
Console.WriteLine("<br>\nEven numbers:");
foreach (int num in evenNumbers) {
Console.Write(num + " ");
}
// Find numbers between 15 and 30
List<int> inRange = numbers.FindAll(x => x >= 15 && x <= 30);
Console.WriteLine("<br>\nNumbers between 15 and 30:");
foreach (int num in inRange) {
Console.Write(num + " ");
}
Console.WriteLine();
}
}
The output of the above code is −
Original numbers: 5 10 15 20 25 30 35 40 Numbers greater than 20: 25 30 35 40 Even numbers: 10 20 30 40 Numbers between 15 and 30: 15 20 25 30
Using FindAll with Custom Objects
Example − Filtering String List
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(String[] args) {
List<string> names = new List<string> { "Alice", "Bob", "Charlie", "Diana", "Edward" };
Console.WriteLine("All names:");
foreach (string name in names) {
Console.WriteLine(name);
}
// Find names starting with specific letter
List<string> startsWithC = names.FindAll(name => name.StartsWith("C"));
Console.WriteLine("\nNames starting with 'C':");
foreach (string name in startsWithC) {
Console.WriteLine(name);
}
// Find names with length greater than 4
List<string> longNames = names.FindAll(name => name.Length > 4);
Console.WriteLine("\nNames with more than 4 characters:");
foreach (string name in longNames) {
Console.WriteLine(name);
}
}
}
The output of the above code is −
All names: Alice Bob Charlie Diana Edward Names starting with 'C': Charlie Names with more than 4 characters: Alice Charlie Diana Edward
Comparison of Filtering Approaches
| Approach | Syntax | Best For |
|---|---|---|
| Method Predicate | list.FindAll(MethodName) |
Complex logic, reusable conditions |
| Lambda Expression | list.FindAll(x => condition) |
Simple conditions, inline filtering |
| LINQ Where | list.Where(x => condition).ToList() |
Chaining operations, deferred execution |
Conclusion
The FindAll method provides an efficient way to filter List elements using predicates. You can use method predicates for complex reusable logic or lambda expressions for simple inline conditions, making it a flexible solution for conditional element retrieval.
