C# All method

The All() method in C# is a LINQ extension method that checks whether all elements in a collection satisfy a given condition. It returns a bool value − true if all elements meet the condition, or false if even one element fails the condition.

The method is available for any IEnumerable<T> collection and is commonly used with arrays, lists, and other collections for validation and filtering operations.

Syntax

Following is the syntax for the All() method −

bool result = collection.All(predicate);

Parameters

  • predicate − A lambda expression or delegate that defines the condition to test each element against.

Return Value

Returns true if all elements satisfy the condition; otherwise, false. For empty collections, it returns true.

Using All() with Arrays

Example

using System;
using System.Linq;

class Demo {
   static void Main() {
      int[] arr = {10, 15, 20};
      
      // checking if all array elements are greater than 5
      bool res = arr.All(val => val > 5);
      Console.WriteLine("All elements > 5: " + res);
      
      // checking if all array elements are greater than 15
      bool res2 = arr.All(val => val > 15);
      Console.WriteLine("All elements > 15: " + res2);
   }
}

The output of the above code is −

All elements > 5: True
All elements > 15: False

Using All() with Lists

Example

using System;
using System.Collections.Generic;
using System.Linq;

class Program {
   static void Main() {
      List<string> names = new List<string> {"Alice", "Bob", "Charlie"};
      
      // check if all names have length > 3
      bool allLongNames = names.All(name => name.Length > 3);
      Console.WriteLine("All names longer than 3 chars: " + allLongNames);
      
      // check if all names start with uppercase
      bool allCapitalized = names.All(name => char.IsUpper(name[0]));
      Console.WriteLine("All names start with uppercase: " + allCapitalized);
   }
}

The output of the above code is −

All names longer than 3 chars: True
All names start with uppercase: True

Using All() with Complex Objects

Example

using System;
using System.Collections.Generic;
using System.Linq;

class Student {
   public string Name { get; set; }
   public int Age { get; set; }
}

class Program {
   static void Main() {
      List<Student> students = new List<Student> {
         new Student { Name = "John", Age = 20 },
         new Student { Name = "Jane", Age = 22 },
         new Student { Name = "Mike", Age = 19 }
      };
      
      // check if all students are adults (age >= 18)
      bool allAdults = students.All(s => s.Age >= 18);
      Console.WriteLine("All students are adults: " + allAdults);
      
      // check if all students are over 21
      bool allOver21 = students.All(s => s.Age > 21);
      Console.WriteLine("All students over 21: " + allOver21);
   }
}

The output of the above code is −

All students are adults: True
All students over 21: False

Empty Collection Behavior

Example

using System;
using System.Linq;

class Program {
   static void Main() {
      int[] emptyArray = new int[0];
      
      // All() returns true for empty collections
      bool result = emptyArray.All(x => x > 100);
      Console.WriteLine("Empty array All() result: " + result);
   }
}

The output of the above code is −

Empty array All() result: True

Conclusion

The All() method is a powerful LINQ extension that validates whether every element in a collection meets a specified condition. It returns false immediately when the first non-matching element is found, making it efficient for validation scenarios. Remember that it returns true for empty collections.

Updated on: 2026-03-17T07:04:35+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements