System.Linq.Enumerable.Any Method

Namespace: System.Linq

Overview

Determines whether any element of a sequence satisfies a condition.

Syntax

Method Description

public static bool Any<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)

Tests whether any element of thesourcesequence satisfies thepredicate.

public static bool Any<TSource>(this IEnumerable<TSource> source)

Tests whether any element of thesourcesequence exists.

Parameters

source<TSource>
An IEnumerable<TSource> to check for elements.
predicate<TSource, bool>
A function to test each element for a condition.

Returns

true if any elements in the source sequence pass the test in the specified predicate, or if there are any elements in the sequence; otherwise, false.

Remarks

TheAnymethod iterates through the sequence and applies the transform in delegatepredicateto each element. If the predicate function returnstruefor any element in the sequence,Anyimmediately returnstrue. If the predicate function returnsfalsefor all elements in the sequence,Anyreturnsfalse. If the sequence is empty,Anyreturnsfalse.

If the sequence is empty and no predicate is specified,Anyreturnsfalse. If the sequence has at least one element and no predicate is specified,Anyreturnstrue.

This method uses deferred execution.

Examples

Example 1: Using Any with a predicate

Example 2: Using Any without a predicate

Extension Methods

This method can be called as an extension method on any object of type IEnumerable<TSource>.

See Also