Why doesn't Any work on a c null object
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
LINQ's Any() works on empty sequences, but it does not work on a null sequence. That distinction matters. An empty collection is a real object with zero elements, while null means there is no sequence object at all. Any() can answer "does this sequence contain at least one item," but it cannot do that when the sequence itself does not exist.
Any() Is an Extension Method, Not Special Null Magic
In source code, items.Any() looks like an instance method call. In reality, LINQ defines Any() as a static extension method on IEnumerable<T>.
This code:
is compiled as if it were something like:
That means the method receives items as an argument named source. If items is null, the method is still called, but the argument value is null.
What Exception You Actually Get
A common misconception is that Any() on null throws NullReferenceException. With LINQ, the usual behavior is different: Enumerable.Any checks its source argument and throws ArgumentNullException.
Example:
This prints ArgumentNullException, not NullReferenceException.
That detail is useful because it tells you where the failure comes from. The method is being called with an invalid argument, not dereferencing a field on a missing object in the usual instance-method sense.
Empty and Null Are Different States
This is the key design distinction:
- empty sequence: valid object, zero items
- null sequence: no sequence object available
Any() is designed to distinguish empty from non-empty, not null from non-null.
This works because empty is a real list. There is an object to query, and the correct answer is false.
Safe Ways to Use Any() with Nullable Sequences
If the sequence might be null, handle that possibility explicitly.
One common pattern is the null-conditional operator:
Here is what happens:
- if
itemsisnull,items?.Any()producesnull - '
null == truebecomesfalse' - if
itemsis notnull,Any()runs normally
Another explicit option is a null check:
That is often the clearest version when the codebase does not rely heavily on nullable-flow shorthand.
Prefer Empty Collections Over Null When Possible
From an API design perspective, returning an empty sequence is usually easier for callers than returning null. If a method logically returns "zero results," an empty collection lets callers write straightforward LINQ without defensive null checks everywhere.
For example:
Now callers can safely do:
That style is usually better than returning null to mean "no results."
Nullability Annotations Help
If nullable reference types are enabled, C# can warn you about calling Any() on something that may be null.
This improves correctness because the compiler can surface the null risk before runtime.
Common Pitfalls
The most common mistake is assuming Any() should treat null the same as an empty sequence. Another is expecting a NullReferenceException when the typical LINQ behavior is actually ArgumentNullException for a null source argument. Developers also sometimes design APIs that return null for "no results" and then spread defensive checks throughout the codebase. A final issue is forgetting that items?.Any() returns a nullable bool, so you usually want == true or ?? false when turning it into a regular boolean.
Summary
- '
Any()works on empty sequences, not onnullsequences.' - LINQ implements
Any()as an extension method that validates itssourceargument. - Calling
Any()onnulltypically results inArgumentNullException. - Use
items?.Any() == trueoritems != null && items.Any()when the sequence may benull. - Prefer returning empty collections instead of
nullwhen designing APIs.

