Bird
0
0

Why does SingleOrDefault() throw an exception when multiple elements match the predicate?

hard🧠 Conceptual Q10 of 15
C Sharp (C#) - LINQ Fundamentals

Why does SingleOrDefault() throw an exception when multiple elements match the predicate?

ABecause it returns default value for multiple matches
BBecause it always returns the first element
CBecause it cannot handle empty collections
DBecause it expects zero or one matching element only
Step-by-Step Solution
Solution:
  1. Step 1: Understand SingleOrDefault() contract

    It expects zero or one element matching the predicate.
  2. Step 2: Behavior on multiple matches

    If more than one element matches, it throws to signal ambiguous result.
  3. Final Answer:

    Because it expects zero or one matching element only -> Option D
  4. Quick Check:

    SingleOrDefault() throws on multiple matches [OK]
Quick Trick: SingleOrDefault() allows zero or one match only [OK]
Common Mistakes:
MISTAKES
  • Thinking it returns default on multiple matches
  • Confusing with FirstOrDefault() behavior
  • Assuming it handles empty collections by throwing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes