Bird
0
0

What does the First() method do in C# when used on a collection?

easy🧠 Conceptual Q1 of 15
C Sharp (C#) - LINQ Fundamentals

What does the First() method do in C# when used on a collection?

AReturns the last element of the collection or null if empty
BReturns the first element of the collection or throws an exception if empty
CReturns all elements except the first one
DReturns the first element or default value if collection is empty
Step-by-Step Solution
Solution:
  1. Step 1: Understand the behavior of First()

    The First() method returns the first element in a collection.
  2. Step 2: Consider empty collection case

    If the collection is empty, First() throws an exception instead of returning a default value.
  3. Final Answer:

    Returns the first element of the collection or throws an exception if empty -> Option B
  4. Quick Check:

    First() behavior = Throws exception if empty [OK]
Quick Trick: First() throws if empty; use FirstOrDefault() to avoid exception [OK]
Common Mistakes:
MISTAKES
  • Confusing First() with FirstOrDefault()
  • Assuming First() returns null if empty
  • Thinking First() returns last element

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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