Checking if a list is empty with LINQ
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
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.
Related reading
- Checking if a list of objects contains a property with a specific value
- Checking if all elements in a list are unique
- Checking if given preorder traversal is valid BST
- Checking if List contains all items from another list
- Choosing between calling asp.net core blazor methods synchronously or asynchronously
- Choosing between MEF and MAF System.AddIn
- Checking if type list in python
- checking subtrees using preorder and inorder strings

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.