Bird
Raised Fist0

Why might using Average() on an empty list cause an error in C#?

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

Why might using Average() on an empty list cause an error in C#?

ABecause Sum() must be called before Average()
BBecause Average() returns zero automatically for empty lists
CBecause Count() must be called before Average()
DBecause Average() cannot compute a value with zero elements and throws an exception
Step-by-Step Solution
Solution:
  1. Step 1: Understand behavior on empty collections

    Calling Average() on an empty list causes an InvalidOperationException because there are no elements to average.
  2. Step 2: Differentiate from other functions

    Sum() returns 0 on empty lists, but Average() throws an error.
  3. Final Answer:

    Because Average() cannot compute a value with zero elements and throws an exception -> Option D
  4. Quick Check:

    Average() on empty list throws exception [OK]
Quick Trick: Average() fails on empty lists, handle carefully [OK]
Common Mistakes:
MISTAKES
  • Assuming Average() returns zero
  • Not checking for empty list
  • Confusing Sum() and Average() behavior

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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