C# | Check if every List element matches the predicate conditions Last Updated : 26 Nov, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 // C# Program to check if every element // in the List matches the conditions // defined by the specified predicate using System; using System.Collections; using System.Collections.Generic; class Geeks { // function which checks whether an // element is even or not. Or you can // say it is the specified condition private static bool isEven(int i) { return ((i % 2) == 0); } // Main Method public static void Main(String[] args) { // Creating a List<T> of Integers List<int> firstlist = new List<int>(); // Adding elements to List for (int i = 0; i <= 10; i+=2) { firstlist.Add(i); } Console.WriteLine("Elements Present in List:\n"); // Displaying the elements of List foreach(int k in firstlist) { Console.WriteLine(k); } Console.WriteLine(" "); Console.Write("Result: "); // Checks if all the elements of firstlist // matches the condition defined by predicate Console.WriteLine(firstlist.TrueForAll(isEven)); } } Output:Elements Present in List: 0 2 4 6 8 10 Result: True Example 2: CSharp // C# Program to check if every element //in the List matches the conditions //defined by the specified predicate 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): "); // Checks if all the elements of lang // matches the condition defined by predicate Console.WriteLine(lang.TrueForAll(EndsWithLanguage)); } // Search predicate returns // true if a string ends in "language". 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: https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.trueforall?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Check if every List element matches the predicate conditions S SanchitDwivedi Follow Improve Article Tags : C# Similar Reads C# | Check if an element is in the Collection<T> Collection<T>.Contains(T) method is used to determine whether an element is in the Collection<T>. Syntax: public bool Contains (T item); Here, item is the object to locate in the Collection<T>. The value can be null for reference types. Return Value: This method return True if item 2 min read C# | How to get all elements of a List that match the conditions specified by the predicate List<T>.FindAll(Predicate<T>) Method is used to get all the elements that match the conditions defined by the specified predicate. Properties of List: It is different from the arrays. A list can be resized dynamically but arrays cannot.List class can accept null as a valid value for refe 3 min read C# | Remove all elements of a List that match the conditions defined by the predicate List<T>.RemoveAll(Predicate<T>) Method is used to remove all the elements that match the conditions defined by the specified predicate. Properties of List: It is different from the arrays. A list can be resized dynamically but arrays cannot. List class can accept null as a valid value fo 3 min read C# | First occurrence in the List that matches the specified conditions List<T>.Find(Predicate<T>) Method is used to search for an element which matches the conditions defined by the specified predicate and it returns the first occurrence of that element within the entire List<T>. Properties of List: It is different from the arrays. A list can be resiz 3 min read C# | Check whether a SortedList object contains a specific key SortedList.Contains(Object) Method is used to check whether a SortedList object contains a specific key. Syntax: public virtual bool Contains (object key); Here, key is the Key which is to be located in the SortedList object. Return Value: This method returns the true if the SortedList object contai 2 min read Like