Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

Enumerable.Any: practical reference

Enumerable.Any: practical reference: Without a predicate it tests whether an element exists; with a predicate it stops at the first true match. It does not count the whole sequence when it can answer earlier.

Version: .NET LINQ; verified on .NET 10.0.14

Syntax

source.Any(/* overload-specific arguments */)

Without a predicate it tests whether an element exists; with a predicate it stops at the first true match. It does not count the whole sequence when it can answer earlier.

Use it instead of Count() > 0 to test whether a deferred source is nonempty and to find quickly whether any match exists.

The predicate may not run at all for an empty source and is not run after the first match.

An empty sequence returns false. An infinite sequence with no match never completes.

The complete program actually uses Enumerable.Any; its exact expected stdout is False.

Examples

using System;
using System.Collections.Generic;
using System.Linq;

Console.WriteLine(Array.Empty<int>().Any());

Result

False

The complete program actually uses `Enumerable.Any`; its exact expected stdout is `False`.

Sources