Tutorialsteacher

Follow Us

Quantifier Operators

The quantifier operators evaluate elements of the sequence on some condition and return a boolean value to indicate that some or all elements satisfy the condition.

OperatorDescription
All Checks if all the elements in a sequence satisfies the specified condition
AnyChecks if any of the elements in a sequence satisfies the specified condition
ContainsChecks if the sequence contains a specific element

All

The All operator evalutes each elements in the given collection on a specified condition and returns True if all the elements satisfy a condition.

Example: All operator C#
IList<Student> studentList = new List<Student>() { 
        new Student() { StudentID = 1, StudentName = "John", Age = 18 } ,
        new Student() { StudentID = 2, StudentName = "Steve",  Age = 15 } ,
        new Student() { StudentID = 3, StudentName = "Bill",  Age = 25 } ,
        new Student() { StudentID = 4, StudentName = "Ram" , Age = 20 } ,
        new Student() { StudentID = 5, StudentName = "Ron" , Age = 19 } 
    };

// checks whether all the students are teenagers    
bool areAllStudentsTeenAger = studentList.All(s => s.Age &gt; 12 && s.Age &lt; 20);

Console.WriteLine(areAllStudentsTeenAger);
Output:
false

Any

Any checks whether any element satisfy given condition or not? In the following example, Any operation is used to check whether any student is teen ager or not.

Example: Any operator C#
bool isAnyStudentTeenAger = studentList.Any(s => s.age &gt; 12 && s.age &lt; 20);
Output:
true

Quantifier operators are Not Supported with C# query syntax.

Learn about quanifier operator - Contains in the next section.