Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q13 of 15
C Sharp (C#) - LINQ Fundamentals

What will be the output of this code?

var list = new List<int> { 5, 10, 15 };
var result = list.SingleOrDefault(x => x == 10);
Console.WriteLine(result);
A10
B0
CThrows exception
Dnull
Step-by-Step Solution
Solution:
  1. Step 1: Understand SingleOrDefault with predicate

    SingleOrDefault returns the only element matching predicate or default if none, throws if multiple.
  2. Step 2: Check list and predicate

    List has one element equal to 10, so SingleOrDefault returns 10.
  3. Final Answer:

    10 -> Option A
  4. Quick Check:

    SingleOrDefault returns single matching element = 10 [OK]
Quick Trick: SingleOrDefault returns single match or default, throws if many [OK]
Common Mistakes:
MISTAKES
  • Expecting 0 as default when element exists
  • Thinking it throws because of multiple elements in list
  • Confusing with FirstOrDefault behavior

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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