Bird
Raised Fist0
C Sharp (C#)programming~10 mins

First, Single, and their OrDefault variants in C Sharp (C#) - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to get the first number from the list.

C Sharp (C#)
var numbers = new List<int> {1, 2, 3};
int first = numbers.[1]();
Drag options to blanks, or click blank then click option'
AFirst
BSingle
CLast
DCount
Attempts:
3 left
💡 Hint
Common Mistakes
Using Single() when there are multiple elements causes an error.
2fill in blank
medium

Complete the code to get the single element from the list that equals 5.

C Sharp (C#)
var numbers = new List<int> {5};
int single = numbers.[1](n => n == 5);
Drag options to blanks, or click blank then click option'
AFirst
BLast
CSingle
DWhere
Attempts:
3 left
💡 Hint
Common Mistakes
Using First() may return the first match even if there are multiple.
3fill in blank
hard

Fix the error in the code to safely get the first element or default if none exists.

C Sharp (C#)
var emptyList = new List<int>();
int firstOrDefault = emptyList.[1]();
Drag options to blanks, or click blank then click option'
ASingleOrDefault
BFirstOrDefault
CFirst
DSingle
Attempts:
3 left
💡 Hint
Common Mistakes
Using First() causes an exception if the list is empty.
4fill in blank
hard

Fill both blanks to get the single element matching the condition or default if none exists.

C Sharp (C#)
var numbers = new List<int> {1, 2, 3};
int result = numbers.[1](n => n == 2).[2]();
Drag options to blanks, or click blank then click option'
AWhere
BFirstOrDefault
CSingleOrDefault
DFirst
Attempts:
3 left
💡 Hint
Common Mistakes
Using SingleOrDefault() directly may throw if multiple matches.
5fill in blank
hard

Fill all three blanks to get the single element matching the condition or default if none exists, using method chaining.

C Sharp (C#)
var words = new List<string> {"apple", "banana", "cherry"};
string word = words.[1](w => w.StartsWith("b")).[2]().[3]();
Drag options to blanks, or click blank then click option'
AWhere
BSingleOrDefault
CToList
DFirstOrDefault
Attempts:
3 left
💡 Hint
Common Mistakes
Skipping ToList() may cause errors if multiple matches exist.

Practice

(1/5)
1.

Which method will throw an exception if the collection does not have exactly one matching element?

First(), FirstOrDefault(), Single(), SingleOrDefault()
easy
A. SingleOrDefault()
B. First()
C. FirstOrDefault()
D. Single()

Solution

  1. Step 1: Understand Single() behavior

    Single() expects exactly one matching element and throws if there are zero or more than one.
  2. Step 2: Compare with other methods

    First() and FirstOrDefault() do not throw if multiple elements exist; SingleOrDefault() throws only if multiple elements exist but returns default if none.
  3. Final Answer:

    Single() -> Option D
  4. Quick Check:

    Throws on multiple matches = Single() [OK]
Hint: Single() throws if more than one match exists [OK]
Common Mistakes:
  • Confusing Single() with First() which returns first match
  • Thinking FirstOrDefault() throws on multiple matches
  • Assuming SingleOrDefault() never throws
2.

Which of the following is the correct syntax to get the first element or default from a list numbers?

var result = numbers._____();
easy
A. Single
B. FirstOrDefault
C. First
D. SingleOrDefault

Solution

  1. Step 1: Identify method for first or default

    FirstOrDefault() returns the first element or default if none found.
  2. Step 2: Check syntax correctness

    numbers.FirstOrDefault() is valid syntax to get first or default.
  3. Final Answer:

    FirstOrDefault -> Option B
  4. Quick Check:

    FirstOrDefault() syntax correct for first or default [OK]
Hint: Use FirstOrDefault() to safely get first or default [OK]
Common Mistakes:
  • Using Single() which throws if multiple elements
  • Using First() which throws if empty
  • Using SingleOrDefault() which expects single element
3.

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);
medium
A. 10
B. 0
C. Throws exception
D. null

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]
Hint: SingleOrDefault returns single match or default, throws if many [OK]
Common Mistakes:
  • Expecting 0 as default when element exists
  • Thinking it throws because of multiple elements in list
  • Confusing with FirstOrDefault behavior
4.

Identify the error in this code snippet:

var items = new List<string> { "apple", "banana", "apple" };
var singleItem = items.Single(x => x == "apple");
Console.WriteLine(singleItem);
medium
A. Returns "apple" without error
B. Returns null because of duplicates
C. Throws InvalidOperationException due to multiple matches
D. Syntax error in lambda expression

Solution

  1. Step 1: Analyze Single() with multiple matches

    Single() throws InvalidOperationException if more than one element matches the predicate.
  2. Step 2: Check list contents

    List has two "apple" strings, so Single() throws exception.
  3. Final Answer:

    Throws InvalidOperationException due to multiple matches -> Option C
  4. Quick Check:

    Single() throws on multiple matches [OK]
Hint: Single() throws if more than one match found [OK]
Common Mistakes:
  • Expecting Single() to return first match
  • Thinking it returns null on duplicates
  • Assuming syntax error in lambda
5.

You have a list of users and want to get the only user with the username "admin" or null if none exists. Which method should you use to avoid exceptions if there are no or multiple admins?

hard
A. FirstOrDefault()
B. SingleOrDefault() with try-catch
C. Single()
D. First()

Solution

  1. Step 1: Understand requirements

    We want the only user named "admin" or null if none, but avoid exceptions if multiple exist.
  2. Step 2: Evaluate methods

    SingleOrDefault() throws if multiple matches, so risky. FirstOrDefault() returns first or null safely even if multiple exist.
  3. Final Answer:

    FirstOrDefault() -> Option A
  4. Quick Check:

    FirstOrDefault() safely returns first or null without exceptions [OK]
Hint: Use FirstOrDefault() to avoid exceptions on multiple matches [OK]
Common Mistakes:
  • Using SingleOrDefault() which throws on multiple matches
  • Using Single() which throws on zero or multiple matches
  • Using First() which throws if none found