LINQ · interview question
What happens when you call First() and FirstOrDefault() on an empty List<string>?
Multiple choiceMedium
Answer and explanation
Correct answer
A. First() throws InvalidOperationException; FirstOrDefault() returns null.
Why
Use First() when an empty sequence is a bug (fail fast). Use FirstOrDefault() when "none" is a valid outcome, and check for default.
Test yourself on LINQ
A free test with instant, explained results. No account needed.