Find and First throws exceptions, how to return null instead?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
This topic causes confusion because Find and First do not behave the same way. List<T>.Find already returns the default value when nothing matches, while LINQ First throws InvalidOperationException if the sequence is empty or no element satisfies the predicate. If you want a null-like result instead of an exception, the usual answer is FirstOrDefault, not First.
Find Versus First
Find belongs to List<T>. First belongs to LINQ and works on any IEnumerable<T>.
If you change the First call to search for a missing value, it throws.
That throws because First promises an element, not an optional result.
Use FirstOrDefault When No Match Is Acceptable
If returning a default value is acceptable, use FirstOrDefault.
For reference types, the default is null. That makes FirstOrDefault the direct replacement most people want.
Value Types Need Extra Care
For value types, the default is not null. It is the type's zero-like value. For int, that is 0. For bool, that is false. That means FirstOrDefault can be ambiguous if 0 is a valid result.
If you need a real null for value types, project into a nullable type.
That makes the "not found" state explicit.
SingleOrDefault and DefaultIfEmpty
Sometimes FirstOrDefault is not the best semantic choice. If the query should return at most one result and duplicates are a bug, SingleOrDefault expresses that intent better. It still returns a default value when nothing matches, but it throws if more than one match exists.
You can also use DefaultIfEmpty when you want to provide a custom fallback object instead of a default value.
That pattern is useful when null would only force more branching later.
Common Pitfalls
The first pitfall is assuming Find throws when no element exists. List<T>.Find does not do that. If code is throwing, the issue is usually First, Single, or another LINQ operator with stricter semantics.
Another common mistake is using FirstOrDefault on a value type and forgetting that the default may be a legitimate business value. In those cases, nullable projection or a custom wrapper is safer.
Developers also sometimes catch InvalidOperationException around First just to simulate optional behavior. That is the wrong abstraction. If missing data is expected, choose FirstOrDefault or SingleOrDefault and let the method communicate intent directly.
Finally, nullable reference types can expose older assumptions. If your project has nullable analysis enabled, pay attention to string? versus string when using FirstOrDefault, because the compiler is correctly telling you the result may be missing.
Summary
- '
List<T>.Findalready returns the default value when nothing matches.' - LINQ
Firstthrows if the sequence has no matching element. - Use
FirstOrDefaultwhen a missing result is acceptable. - For value types, default values can be ambiguous, so nullable projection may be better.
- Pick
First,FirstOrDefault, orSingleOrDefaultbased on the semantic rule you actually need.

