Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
C# | How to get all elements of a List that match the conditions specified by the predicate
Next article icon

C# | Check if every List element matches the predicate conditions

Last Updated : 26 Nov, 2022
Comments
Improve
Suggest changes
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


Next Article
C# | How to get all elements of a List that match the conditions specified by the predicate

S

SanchitDwivedi
Improve
Article Tags :
  • C#

Similar Reads

  • C# | Check if the SortedSet contains a specific element
    SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. SortedSet<T>.Contains(T) Method is used to check if a SortedSet contains a specific element or not. Properties: In C#, SortedSet class can be used to store, remov
    2 min read
  • C# | Check if a HashSet contains the specified element
    A HashSet is an unordered collection of the unique elements. It is found in System.Collections.Generic namespace. It is used in a situation where we want to prevent duplicates from being inserted in the collection. As far as performance is concerned, it is better in comparison to the list. HashSet.C
    2 min read
  • 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
  • C# Program to Demonstrate the Use of the Method as a Condition in the LINQ
    LINQ is known as Language Integrated Query and it is introduced in .NET 3.5. It gives the ability to .NET languages to generate queries to retrieve data from the data source. It removes the mismatch between programming languages and databases and the syntax used to create a query is the same no matt
    2 min read
  • C# | Check if a SortedList object contains a specific value
    SortedList class is a collection of (key, value) pairs which are sorted according to keys. Those pairs can be accessible by key and as well as by index(zero-based indexing). This comes under System.Collections namespace. SortedList.ContainsValue(Object) method is used to check whether a SortedList o
    2 min read
  • C# | Check if ListDictionary contains a specific key
    ListDictionary.Contains(Object) method is used to check whether the ListDictionary contains a specific key or not. Syntax: public bool Contains (object key); Here, key is the key to locate in the ListDictionary. Return Value: The method returns true if the ListDictionary contains an entry with the s
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences