Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q6 of 15
C Sharp (C#) - LINQ Fundamentals
Identify the error in this code snippet:
var nums = new List<int> { 4, 2, 7 };
var sorted = nums.OrderBy(n => n);
sorted.Sort();
AList<int> does not support OrderBy
BOrderBy returns IEnumerable, which has no Sort method
CSort method requires a lambda expression
DOrderBy modifies the original list, so Sort is redundant
Step-by-Step Solution
Solution:
  1. Step 1: Understand return type of OrderBy

    OrderBy returns an IEnumerable<T>, not a List<T>.
  2. Step 2: Check if IEnumerable has Sort method

    IEnumerable does not have a Sort method, so calling sorted.Sort() causes an error.
  3. Final Answer:

    OrderBy returns IEnumerable, which has no Sort method -> Option B
  4. Quick Check:

    OrderBy returns IEnumerable, no Sort method [OK]
Quick Trick: OrderBy returns IEnumerable, not List; no Sort method [OK]
Common Mistakes:
MISTAKES
  • Assuming OrderBy returns List
  • Trying to call Sort on IEnumerable
  • Thinking OrderBy modifies original list

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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