TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Array.TrueForAll Method

This C# example program uses the Array.TrueForAll method. TrueForAll tests elements with a Predicate.

Array.TrueForAll is a static Array method.

It gives you a declarative way to test every element in your array for some condition using a Predicate. It scans the array and returns true or false.

Predicate

Example. First, this example code demonstrates the Array.TrueForAll method in two contexts. The first test uses an int array of four odd numbers. The Array.TrueForAll method is invoked and the predicate is specified.

Int Array

Predicate: The predicate method returns true whenever a number is not evenly divisible by two.

Odd, Even

Lambda expression: For more information on the syntax used, please see the lambda expression article.

C# program that uses Array.TrueForAll

using System;

class Program
{
    static void Main()
    {
	{
	    int[] values = { 1, 3, 5, 7 };
	    bool result = Array.TrueForAll(values, y => y % 2 == 1);
	    Console.WriteLine(result);
	}
	{
	    int[] values = { 2, 5, 8 };
	    bool result = Array.TrueForAll(values, y => y % 2 == 1);
	    Console.WriteLine(result);
	}
    }
}

Output

True
False

In the second part, the numbers in the array are not all odd. Therefore the same Array.TrueForAll parameters will have the method return false. We see that Array.TrueForAll correctly applies the predicate to every element in the array.

Discussion. Obviously, you could imperatively code a method that does the same thing as Array.TrueForAll. Simply test each element in foreach-loop and return early if an element doesn't match.

Foreach

Typically, this sort of approach is much faster than using a call such as Array.TrueForAll. Therefore, in performance work, using a foreach or for-loop is better. On the other hand, Array.TrueForAll yields more concise code in some cases.

For

However: Array.TrueForAll is less familiar to programmers used to other languages. Its use might make a development team less efficient.

Summary. We examined the Array.TrueForAll method. By applying the predicate argument to each element in the array argument, you can use it to test every element of the array for some condition without using a loop construct at all.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf