List<T>.TrueForAll(Predicate<T>) is used to check whether every element in the List<T> matches the conditions defined by the specified predicate or not. Syntax:
public bool TrueForAll (Predicate<T> match);
Parameter:
match: It is the Predicate<T> delegate which defines the conditions to check against the elements.
Return Value: This method returns true if every element in the List<T> matches the conditions defined by the specified predicate otherwise it returns false. If the list has no elements, the return value is true. Exception: This method will give ArgumentNullException if the match is null. Below programs illustrate the use of List<T>.TrueForAll(Predicate<T>) Method: Example 1:
CSharp
using System;
using System.Collections;
using System.Collections.Generic;
class Geeks {
private static bool isEven( int i)
{
return ((i % 2) == 0);
}
public static void Main(String[] args)
{
List< int > firstlist = new List< int >();
for ( int i = 0; i <= 10; i+=2) {
firstlist.Add(i);
}
Console.WriteLine("Elements Present in List:\n");
foreach ( int k in firstlist)
{
Console.WriteLine(k);
}
Console.WriteLine(" ");
Console.Write("Result: ");
Console.WriteLine(firstlist.TrueForAll(isEven));
}
}
|
Output:
Elements Present in List:
0
2
4
6
8
10
Result: True
Example 2:
CSharp
using System;
using System.Collections;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
List< string > lang = new List< string >();
lang.Add("C# language");
lang.Add("C++ language");
lang.Add("Java language");
lang.Add("Python language");
lang.Add("Ruby language");
Console.WriteLine("Elements Present in List:\n");
foreach ( string language in lang)
{
Console.WriteLine(language);
}
Console.WriteLine(" ");
Console.Write("TrueForAll(EndsWithLanguage): ");
Console.WriteLine(lang.TrueForAll(EndsWithLanguage));
}
private static bool EndsWithLanguage(String s)
{
return s.ToLower().EndsWith("language");
}
}
|
Output:
Elements Present in List:
C# language
C++ language
Java language
Python language
Ruby language
TrueForAll(EndsWithLanguage): True
Reference:
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
26 Nov, 2022
Like Article
Save Article