CSharp - LINQ All

Introduction

The All operator returns true if every element in the input sequence matches a condition.

Prototypes

public static bool All<T>(
  this IEnumerable<T> source,
       Func<T, bool> predicate);

The All operator enumerates the source input sequence and returns true only if the predicate returns true for every element in the sequence.

Once the predicate returns false, the enumeration will stop.

Exceptions

ArgumentNullException is thrown if any of the arguments are null.

The following code uses a predicate with which we know at least some of the elements will return false.

Demo

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
class Program/*from   w  w w.  j  a v a  2  s.  c  o m*/
{
    static void Main(string[] args)
    {
          string[] codeNames = {"Python", "Java", "Javascript", "Bash", "C++", "Oracle"};
    
          bool all = codeNames.All(s => s.Length > 5);
          Console.WriteLine(all);
        
           all = codeNames.All(s => s.Length > 3);
          Console.WriteLine(all);
    }
}

Result