Checking if a list is empty with LINQ
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Checking whether a list is empty in C# does not require LINQ in most cases. For concrete collections like List and arrays, use Count == 0 or Length == 0. LINQ Any() is best when working with IEnumerable where counting may be expensive or unavailable.
Prefer Direct Properties on Concrete Collections
If the variable type is List<T>, checking Count is simple and fast.
For arrays, use Length == 0.
Use Any() for IEnumerable
IEnumerable<T> may represent streams, generators, or deferred queries where counting all items is unnecessary. Any() short-circuits as soon as first element is found.
For remote query providers, Any() also translates efficiently in many cases.
Null-safe Empty Checks
Always handle null references if collection may be optional. Extension patterns keep call sites clean.
This avoids repeated null guards throughout business logic.
Performance and Readability Tradeoffs
For in-memory lists, Count communicates intent clearly and avoids LINQ dependency. For polymorphic abstractions, Any() communicates "existence" and can prevent expensive full enumeration. Pick based on actual type and cost model.
Avoid writing Count() == 0 on an IEnumerable unless you know provider behavior. It can force full traversal and hurt performance on large or streaming inputs.
API Consistency in Shared Libraries
In shared helper libraries, define one emptiness-check convention and apply it consistently. For example, expose IsNullOrEmpty for IEnumerable<T> and use direct Count checks for concrete types inside performance-critical code paths. Consistency reduces review noise and prevents subtle behavioral differences.
This helper reads naturally at call sites and captures null handling in one place.
Measuring Before Optimizing
If emptiness checks appear in hot loops, benchmark realistic workloads instead of assuming one method is always faster. The best choice depends on runtime type and provider semantics.
Performance data plus clear conventions gives teams both speed and readability.
In code reviews, prefer style rules that match data type intent. If the variable is clearly a list, Count reads immediately and avoids extra imports. If it is an abstraction or query, Any() communicates intent better and often avoids full enumeration. Intent-focused checks improve maintainability.
Document these rules in team standards so usage remains consistent across repositories.
Clear conventions lower maintenance cost in large teams.
Common Pitfalls
- Using LINQ
Count()to check emptiness on sequences that require full enumeration. - Calling
Any()repeatedly on one-time iterators and consuming data unexpectedly. - Forgetting null checks when collection references are optional.
- Mixing
List<T>andIEnumerable<T>patterns without considering runtime type. - Optimizing prematurely without measuring hot-path behavior.
Summary
- Use
Count == 0forList<T>andLength == 0for arrays. - Use
Any()forIEnumerable<T>when emptiness is the only question. - Prefer null-safe helper extensions in shared code.
- Avoid
Count()emptiness checks on deferred or streaming sources. - Choose the pattern that matches collection type and performance characteristics.

