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
Check if every List element matches the predicate conditions in C#
To check if every List element matches the predicate conditions in C#, you can use the List<T>.TrueForAll() method. This method takes a predicate delegate and returns true if all elements in the list satisfy the condition, or false if any element fails the test.
Syntax
Following is the syntax for the TrueForAll() method −
public bool TrueForAll(Predicate<T> match)
Parameters
match: A delegate that defines the conditions to check against the elements. It takes an element of type T and returns a boolean.
Return Value
Returns true if every element in the list matches the predicate conditions, otherwise false. Returns true for an empty list.
Using TrueForAll() with Custom Predicate Method
You can define a custom method as a predicate to check specific conditions −
Example - Checking Divisibility 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);
}
bool result = list.TrueForAll(IsMultipleOfTen);
Console.WriteLine("All elements divisible by 10: " + result);
}
}
The output of the above code is −
List elements... 200 215 310 500 600 All elements divisible by 10: False
Example - All Elements Match Condition
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(100);
list.Add(200);
list.Add(300);
list.Add(400);
list.Add(500);
list.Add(600);
list.Add(700);
list.Add(800);
list.Add(900);
list.Add(1000);
Console.WriteLine("List elements...");
foreach (int i in list) {
Console.WriteLine(i);
}
bool result = list.TrueForAll(IsMultipleOfTen);
Console.WriteLine("All elements divisible by 10: " + result);
}
}
The output of the above code is −
List elements... 100 200 300 400 500 600 700 800 900 1000 All elements divisible by 10: True
Using TrueForAll() with Lambda Expression
You can also use lambda expressions for inline predicate conditions −
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(String[] args) {
List<string> names = new List<string> { "Alice", "Bob", "Charlie", "David" };
// Check if all names have length greater than 2
bool allLongNames = names.TrueForAll(name => name.Length > 2);
Console.WriteLine("All names longer than 2 characters: " + allLongNames);
// Check if all names start with uppercase letter
bool allCapitalized = names.TrueForAll(name => char.IsUpper(name[0]));
Console.WriteLine("All names start with uppercase: " + allCapitalized);
List<int> numbers = new List<int> { 2, 4, 6, 8, 10 };
bool allEven = numbers.TrueForAll(num => num % 2 == 0);
Console.WriteLine("All numbers are even: " + allEven);
}
}
The output of the above code is −
All names longer than 2 characters: True All names start with uppercase: True All numbers are even: True
Conclusion
The List<T>.TrueForAll() method provides an efficient way to check if all elements in a list satisfy a given condition. You can use it with custom predicate methods or lambda expressions to validate data according to your specific requirements.
