C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Argument: This lambda receives 1 parameter (the type of element in the collection), and returns 1 bool (whether it matches the condition).
ExtensionPredicateBoolInfo: We invoke All() on the integer array with 3 different lambdas. The All() method returns true, false and then true again.
C# program that uses All method
using System;
using System.Linq;
class Program
{
static void Main()
{
int[] array = { 10, 20, 30 };
// Are all elements >= 10? YES
bool a = array.All(element => element >= 10);
// Are all elements >= 20? NO
bool b = array.All(element => element >= 20);
// Are all elements < 40? YES
bool c = array.All(element => element < 40);
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
}
}
Output
True
False
True
Length: We select all strings from the List that have lengths of exactly 4 characters.
WhereString LengthAll: This returns true if all elements in the query result are equal to their uppercase forms (in other words, are uppercase strings).
True: The All() method returns true because all 4-letter strings are uppercase strings.
True, FalseC# program that uses List, query and All
using System;
using System.Linq;
using System.Collections.Generic;
class Program
{
static void Main()
{
var colors = new List<string>() { "BLUE", "GREY", "white" };
// True if all strings of length 4 are in uppercase.
var result = (from color in colors
where color.Length == 4
select color).All(element => element == element.ToUpper());
Console.WriteLine("RESULT: {0}", result);
}
}
Output
RESULT: True
Also: All() is probably slower due to the requirement that a Func instance be created.
FuncNote: Performance is often more important than fancy syntax—this depends on the program.