C#
NullReferenceException
LINQ
Programming
Any()

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:

csharp
1using System.Collections.Generic;
2using System.Linq;
3
4List<int> items = new List<int> { 1, 2, 3 };
5bool hasAny = items.Any();

is compiled as if it were something like:

csharp
bool hasAny = Enumerable.Any(items);

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:

csharp
1using System;
2using System.Collections.Generic;
3using System.Linq;
4
5List<int> items = null;
6
7try
8{
9    bool hasAny = items.Any();
10    Console.WriteLine(hasAny);
11}
12catch (Exception ex)
13{
14    Console.WriteLine(ex.GetType().Name);
15}

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.

csharp
1using System;
2using System.Collections.Generic;
3using System.Linq;
4
5var empty = new List<int>();
6Console.WriteLine(empty.Any()); // False

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:

csharp
1using System.Collections.Generic;
2using System.Linq;
3
4List<int> items = null;
5bool hasAny = items?.Any() == true;

Here is what happens:

  • if items is null, items?.Any() produces null
  • 'null == true becomes false'
  • if items is not null, Any() runs normally

Another explicit option is a null check:

csharp
bool hasAny = items != null && items.Any();

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:

csharp
1using System.Collections.Generic;
2
3IEnumerable<string> GetNames(bool includeData)
4{
5    if (!includeData)
6        return new List<string>();
7
8    return new List<string> { "Alice", "Bob" };
9}

Now callers can safely do:

csharp
bool hasNames = GetNames(false).Any();

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.

csharp
1#nullable enable
2using System.Collections.Generic;
3using System.Linq;
4
5IEnumerable<int>? numbers = null;
6bool hasAny = numbers?.Any() == true;

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 on null sequences.'
  • LINQ implements Any() as an extension method that validates its source argument.
  • Calling Any() on null typically results in ArgumentNullException.
  • Use items?.Any() == true or items != null && items.Any() when the sequence may be null.
  • Prefer returning empty collections instead of null when designing APIs.

Course illustration
Course illustration

All Rights Reserved.