Why does .NET foreach loop throw NullRefException when collection is null?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
foreach in C# does not treat null as an empty collection. If the collection reference is null, the runtime throws NullReferenceException because the loop still needs an enumerator object and there is nothing to call it on. The confusion comes from foreach looking like a language primitive when it is really syntax that compiles down to method calls on the collection.
What foreach Expands To
Given this code:
the compiler turns it into logic similar to this:
If items is null, the call to items.GetEnumerator() dereferences a null reference, so the loop fails before the first iteration.
That is why foreach on null behaves differently from foreach on an empty list. An empty list still has a valid enumerator. A null reference does not.
Guard the Collection Explicitly
The simplest fix is a null guard:
This is good when null means "there is no collection" and you want that meaning to stay visible in the code.
If you want to treat null as empty, coalesce to an empty sequence:
That keeps the loop body simple and removes the exception.
Prefer Non-Null Collections by Design
The best fix is often not a guard at the loop site. It is making sure collections are initialized and stay non-null throughout the object lifetime.
Now callers can safely iterate:
Returning empty collections instead of null is usually the more ergonomic API design because callers do not need to special-case the absence of items.
Nullable Reference Types Make the Risk Obvious
With nullable reference types enabled, the compiler can help highlight risky loops:
Iterating directly over names should prompt a warning because it may be null. That warning is useful. Do not silence it blindly. Decide whether the collection should be nullable at all.
If null is a valid state, handle it explicitly. If it is not, redesign the API so the property or return value is always initialized.
Use the Right Semantics for Your Domain
There is a meaningful difference between:
- no collection object was provided
- a collection was provided but it contains zero items
Some domains care about that distinction. For example, a partial update payload may use null to mean "do not touch this field" and an empty list to mean "clear it." In that case, replacing null with empty too early could lose information.
So the practical rule is:
- return empty collections when the caller only needs iteration
- keep
nullonly when it conveys business meaning
Common Pitfalls
- Assuming
foreachtreatsnullthe same way it treats an empty list. - Adding null coalescing everywhere instead of fixing an API that should never return null collections.
- Ignoring the semantic difference between null and empty in request models.
- Disabling nullable warnings instead of deciding on a clear collection contract.
- Catching
NullReferenceExceptionafter the fact instead of preventing the null dereference.
Summary
- '
foreachthrows on null because it must callGetEnumerator()on the collection reference.' - An empty collection is safe because it still provides a valid enumerator.
- Use null guards or
Enumerable.Empty<T>()when null should behave like empty. - Prefer API designs that initialize collections instead of returning null.
- Keep null collections only when null carries real domain meaning.

