Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

C# Dictionary

  1. Home
  2. C# Dictionary
  3. Enumerable.Any() / All() / Contains()

Enumerable.Any() / All() / Contains()

LINQ extension methods for checking conditions or the presence of elements in a sequence.

Syntax

using System.Linq;

// Whether at least one element exists (returns false for an empty sequence)
bool any = source.Any();
bool any = source.Any(x => condition);

// Whether all elements satisfy the condition (returns true for an empty sequence)
bool all = source.All(x => condition);

// Whether the sequence contains the specified value
bool has = source.Contains(value);
bool has = source.Contains(value, IEqualityComparer<T>);

Method List

MethodDescription
Any()Returns true if the sequence contains at least one element.
Any(predicate)Returns true if at least one element satisfies the condition.
All(predicate)Returns true if all elements satisfy the condition. Always returns true for an empty sequence.
Contains(value)Returns true if the specified value is found in the sequence.
Contains(value, comparer)Checks whether a value exists using a custom equality comparer.
SequenceEqual(second)Returns true if two sequences are equal in both order and elements.

Sample Code

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

List<int> scores = new List<int> { 80, 95, 70, 88, 60, 92 };

// Any: checks whether at least one element satisfies the condition.
Console.WriteLine(scores.Any());              // true (sequence is not empty)
Console.WriteLine(scores.Any(s => s >= 90)); // true (a score of 90 or higher exists)
Console.WriteLine(scores.Any(s => s > 100)); // false (no score exceeds 100)

// All: checks whether every element satisfies the condition.
Console.WriteLine(scores.All(s => s >= 60)); // true (all scores are 60 or higher)
Console.WriteLine(scores.All(s => s >= 80)); // false (some scores are below 80)

// Contains: checks whether a specific value is in the sequence.
Console.WriteLine(scores.Contains(88));       // true
Console.WriteLine(scores.Contains(100));      // false

// Behavior with an empty sequence
List<int> empty = new List<int>();
Console.WriteLine(empty.Any());               // false
Console.WriteLine(empty.All(n => n > 0));    // true (always true for empty sequences)

// Using Contains with a string list, ignoring case.
List<string> fruits = new List<string> { "Apple", "Banana", "Cherry" };
bool hasApple = fruits.Contains("apple", StringComparer.OrdinalIgnoreCase);
Console.WriteLine(hasApple); // true

// Using SequenceEqual to check whether two sequences are identical.
List<int> a = new List<int> { 1, 2, 3 };
List<int> b = new List<int> { 1, 2, 3 };
List<int> c = new List<int> { 3, 2, 1 };
Console.WriteLine(a.SequenceEqual(b)); // true (same order and values)
Console.WriteLine(a.SequenceEqual(c)); // false (different order)

Notes

Any() and All() use short-circuit evaluation. Any() stops iterating as soon as it finds an element that satisfies the condition, and All() stops as soon as it finds one that does not.

All() always returns true for an empty sequence (vacuous truth). If you need to guard against empty sequences, use Any() first to confirm the sequence has elements.

Using Any() is more efficient than checking element count for an emptiness check. For counting elements, see Enumerable.Count(). For filtering by a condition, see Enumerable.Where().

If you find any errors or copyright issues, please .