TheDeveloperBlog.com

Home | Contact Us

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

C# Any Method

This C# example program uses the Any method from the System.Linq namespace.

Any receives a Predicate.

It determines if any element in a collection matches a certain condition. You could do this imperatively, using a loop construct. But the Any extension method provides another way. It reduces code size.

Extension Method

Example. Please include the System.Linq using directive at the top of your program. This allows you to call the Any extension. In this example, we see an array of three integer values. These values determine the results of the Any method.

Int Array

Methods: The first call tests for any even int. The second tests for any int greater than 3. The third checks for any int equal to 2.

Odd, Even

Tip: You can, when executing the program on your computer, change the expressions in the lambda to determine the correctness of the tests.

Based on:

.NET 4.5

C# program that uses Any extension method

using System;
using System.Linq;

class Program
{
    static void Main()
    {
	int[] array = { 1, 2, 3 };
	// See if any elements are divisible by two.
	bool b1 = array.Any(item => item % 2 == 0);
	// See if any elements are greater than three.
	bool b2 = array.Any(item => item > 3);
	// See if any elements are 2.
	bool b3 = array.Any(item => item == 2);
	// Write results.
	Console.WriteLine(b1);
	Console.WriteLine(b2);
	Console.WriteLine(b3);
    }
}

Output

True
False
True

Internals. How does the Any method actually work? When you call the Any method, you are passing a Predicate type, which is a function with a bool result. Internally, the Any method loops through each element in the source collection.

Predicate

Then: When it finds an element that the Predicate returns true for, the true result is propagated. It uses an early-exit.

Summary. The Any method in the C# language doesn't do just anything. It instead evaluates a Predicate method on the source collection and returns a boolean indicating whether any element matches the Predicate.

Thus: The Any method elegantly replaces imperative loop constructs. It reduces the size of source code.


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