TheDeveloperBlog.com

Home | Contact Us

CSharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript

<< Back to C-SHARP

C# All Method: All Elements Match a Condition

Use the All extension method from System.Linq to determine if all elements match a condition.
All. This is an extension method. It tells us if all the elements in a collection match a certain condition. It returns true or false.
System.Linq. All() is part of the System.Linq namespace in the .NET Framework. We often want to call All() on the result of a query expression using other LINQ features.LINQ
An example. To begin, include the System.Linq namespace. Next, pass in a lambda expression to the All method. We pass 1 argument to the All() method, usually a lambda expression.

Argument: This lambda receives 1 parameter (the type of element in the collection), and returns 1 bool (whether it matches the condition).

ExtensionPredicateBool

Info: 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
List, query example. We can use All() on a query expression to create more complex meanings. Here we use All() on a List generic type.List

Length: We select all strings from the List that have lengths of exactly 4 characters.

WhereString Length

All: 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, False
C# 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
Benefits. Why would we use All() instead of looping over elements and using an if-statement? The All method is more compact. And it can be part of a more complex LINQ statement.

Also: All() is probably slower due to the requirement that a Func instance be created.

Func

Note: Performance is often more important than fancy syntax—this depends on the program.

A summary. As part of the LINQ extensions, All() serves a specific purpose. For arrays, we can also use the Array.TrueForAll method, which uses the same syntax but is a static method.Array.TrueForAllStatic
© TheDeveloperBlog.com
The Dev Codes