Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

C# Any() vs Count(): Which One Should You Use?

Choosing between Any() and Count() in C# depends on whether you need an existence check or an exact count. See performance and edge-case guidance.

~5 min readC#
#LINQ#Performance#Collections#C#
A visual comparison between Any() and Count() in C# showing a collection with a checkmark for Any and a number counter for Count.

Choosing between Any() and Count() in C# comes down to whether you need an existence check or an exact count. Any() is normally the right tool for checking whether a sequence is non-empty; Count() is necessary when you need the number of items. The difference is especially important for lazy IEnumerable<T> sequences because Count() > 0 can force a full enumeration.

Checking for Elements: The Core Difference

When working with collections in C#, a common task is to determine whether a collection contains any elements. Any() returns a bool indicating whether the sequence contains at least one element, and it short-circuits as soon as it finds the first element. Count() returns an int; for lazy sequences it enumerates every item to produce a total. Comparing Count() > 0 can therefore require a full enumeration where Any() stops early.

csharp
IEnumerable<int> numbers = GetNumbers(); // Some lazy sequence bool hasAny = numbers.Any(); // Short-circuits after the first element bool hasCount = numbers.Count() > 0; // Enumerates the entire sequence

For a non-empty sequence, Any() returns after the first MoveNext(). Count() has to keep going until the sequence is exhausted. For an infinite sequence, Count() would never return, while Any() would return true immediately.

Performance Is Type-Dependent

Performance is the most often cited reason to prefer Any(), but the size of the difference depends on the underlying type. For types that implement ICollection<T>—such as List<T> and arrays—Count() has an optimization: it returns the Count property directly rather than enumerating the sequence. In those cases, Count() and Any() have comparable cost.

For any IEnumerable<T> that does not provide that fast count—such as the result of a Select() projection, a Where() filter, or a custom iterator—Count() must iterate the entire sequence. Any() needs only one element, so for a non-empty sequence its cost is constant, while Count() is O(n).

csharp
IEnumerable<int> largeSequence = Enumerable.Range(0, 1_000_000); bool isEmpty = !largeSequence.Any(); // Stops after the first element bool isNotEmpty = largeSequence.Count() > 0; // Enumerates a million elements

The same applies to filtered sequences:

csharp
IEnumerable<Order> matchingOrders = allOrders.Where(o => o.Status == "Pending"); bool hasPending = matchingOrders.Any(); // Stops at the first match bool hasPendingCount = matchingOrders.Count() > 0; // Counts every match

Calling Count() forces all matching orders to be counted even though the only question is whether at least one exists. If there are no matches, both operations reach the end of the filtered sequence, but Any() still expresses the intent more directly. For large or lazily evaluated sequences, Any() is the safer choice for existence checks.

Correctness and Edge Cases

Beyond performance, there are correctness reasons to avoid Count() for emptiness checks.

Infinite sequences

csharp
IEnumerable<int> infinite = InfiniteSequence(); // Hypothetical infinite iterator bool hasAny = infinite.Any(); // Returns true after the first element bool hasCount = infinite.Count() > 0; // Never terminates

Most production code does not use infinite sequences directly, but this example shows that Count() assumes a finite sequence; Any() does not.

Side effects and single-use sequences

If a sequence is produced by an iterator that performs I/O or changes state, enumerating it can be destructive. Count() will traverse the entire sequence, so an emptiness check can consume every element. Any() stops after the first element if one exists, so it can avoid most of that work; if the sequence is empty, it still reaches the end. However, if the source is a single-use iterator, the first element may already have been consumed by the time you iterate again. For one-pass sequences, avoid checking with one method and then re-enumerating: either materialize the results first or structure the code so the existence check and the processing happen in a single pass.

Best Practices: Emptiness Checks vs. Exact Counts

Use Any() when you only need to know whether the sequence contains at least one element. if (collection.Any()) reads directly as “do something if the collection is not empty” and does not force a full count.

Use Count() when you need the actual number of items, such as to display a total, allocate an array, or enforce a business rule based on the exact count.

ScenarioRecommended MethodReason
Check whether a sequence is non-emptyAny()Short-circuits on the first element
Check whether a sequence is empty!Any()Same behavior, negated
Get the number of itemsCount()Returns an integer count
Check whether there are more than N itemsSkip(N).Any()Avoids counting the entire lazy sequence

For the last row, if the source is already a List<T> or array, using the Count or Length property is also O(1). The Skip(N).Any() approach is most useful when you are working with a lazy IEnumerable<T> and you do not need the exact count.

Note that these recommendations apply to the LINQ extension methods. For a concrete List<T>, you can use the Count property (list.Count > 0) which is O(1). For an array, array.Length > 0 is direct. When a method accepts IEnumerable<T>, Any() is the standard emptiness check.

Count Property vs. Count() Method

It is important to distinguish the Count property, available on collection types like List<T> and Dictionary<TKey, TValue>, from the LINQ Count() extension method. The property is an O(1) operation. The method may or may not be optimized depending on the runtime type.

csharp
List<int> list = new List<int>(); bool hasItems = list.Count > 0; // Property access—O(1) bool hasItemsLinq = list.Any(); // LINQ—also fast for this type

For List<T>, both are acceptable. However, when a method parameter is typed as IEnumerable<T>, there is no Count property on the interface. You must choose between Count() and Any(); Any() is preferred when you only need an existence check.

Advanced Scenarios and Limitations

Parallel LINQ (PLINQ)

With AsParallel(), Any() can stop as soon as one partition finds an element. Count() must combine counts from all partitions, so it generally does more work. If you only need an existence check, Any() remains the better fit.

Custom iterators

If you implement an iterator that has side effects or is single-use, be careful with any enumeration. Any() calls MoveNext() once and stops for a non-empty sequence; Count() calls MoveNext() until the sequence is exhausted. Neither method is a non-destructive peek. If the same sequence will be enumerated later, materialize it first or design the iteration so the existence check and processing happen together.

Database-backed LINQ

In Entity Framework or LINQ-to-Entities, Any() is typically translated to an EXISTS query, which can be more efficient than SELECT COUNT(*) followed by a comparison. Prefer Any() when the query only needs to know whether rows exist.

In summary, use Any() for emptiness checks and Count() when you actually need the number of items. That advice applies to both in-memory LINQ and database-backed queries, and it keeps performance and intent aligned.

Try matching browser tools

All tools

Small utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.

All C# guides

More in C#

All C# guides

The guides filed next to this one in C#, in library order.