User Tools

Site Tools


c_sharp_any

C# any - C Sharp dot NET any

Return to C Sharp Z, C# .NET, Computer Science Topics, C# .NET Glossary, Awesome C Sharp, C Sharp dot NET Topics

Any Method - Use the Any method from the System.Linq namespace. Any returns true if a match exists in a collection.

C#

This page was last reviewed on Nov 16, 2021.

Any. This C# method receives a Predicate. It determines if a matching element exists in a collection. We could do this with a loop construct.

Shows a method

A simple method. The Any extension method provides another way to check for a matching element. It has some benefits—it can reduce code size.

Predicate

Extension

Example code. Add 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 3 integer values.

Info In the program, the 3 values (1, 2 and 3) determine the results of the Any method.

Int Array

Detail 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 change the expressions in the lambda to determine the correctness of the tests.

Lambda

Shows a 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);
   }
}

True False True

Internals. How does the Any method work? When you call the Any method, you are passing a Predicate type, which is a function with a bool result. And Internally, the Any method loops through each element in the source collection. Then When it finds an element that the Predicate returns true for, the true result is propagated. It uses an early-exit.

A summary. The Any method evaluates a Predicate method on the source collection. It returns a boolean indicating whether any element matches the Predicate.

Fair Use Sources

C Sharp: C# Fundamentals, C# Inventor - C# Language Designer: Anders Hejlsberg of Microsoft in January 2000, Now Mads Torgersen is Primary Architect; Dot Net, C# Keywords, C# on Linux, C# on macOS, C# on Windows, C# on Android, C# on iOS, C# Installation (choco install dotnet, brew install dotnet), C# Containerization ( C# with Docker, C# with Podman, C# and Kubernetes), C# Built-In Data Types, C# Data Structures - C# Algorithms, C# Syntax, C# OOP - C# Design Patterns, Clean C# - C# Style Guide, C# Best Practices ( C# Core Guidelines (CG), ) - C# BDD, C# Compiler, C# IDEs (Visual Studio, Visual Studio Code, JetBrains Ryder), C# Development Tools, C# Linter, C# Debugging, C# Modules, C# Packages, C# Package Manager (NuGet), C# Standard Library, C# libraries, C# frameworks, C# DevOps - C# SRE, C# .NET and Databases (LINQ and Entity Framework ORM), C# Data Science - C# DataOps, C# Machine Learning - ML.NET, C# Deep Learning, Functional C#, C# Concurrency, C# Parallel Programming, Async C#, C# History, C# Bibliography, Manning C Sharp Series, C# Courses, C# Glossary, C# Versions, C# Topics, C# Research, C# GitHub, Written in C#, C# Popularity, C# Awesome. (navbar_csharp - see also navbar_csharp_versions, navbar_dotnet_versions, navbar_csharp_libraries, navbar_csharp_standard_library, navbar_fsharp)


Cloud Monk is Retired (for now). Buddha with you. © 2005 - 2024 Losang Jinpa or Fair Use. Disclaimers

SYI LU SENG E MU CHYWE YE. NAN. WEI LA YE. WEI LA YE. SA WA HE.


c_sharp_any.txt · Last modified: 2023/05/21 19:06 by 127.0.0.1