These methods help you find one item in a list or collection easily. They make your code simple and clear when you want just one element.
First, Single, and their OrDefault variants in C Sharp (C#)
Start learning this pattern below
Jump into concepts and practice - no test required
collection.First(); collection.FirstOrDefault(); collection.Single(); collection.SingleOrDefault();
First() returns the first item and throws an error if none found.
Single() expects exactly one item and throws if zero or more than one found.
var firstName = names.First();
var firstOrDefault = names.FirstOrDefault();
var singleUser = users.Single(u => u.Id == 5);var singleOrDefaultUser = users.SingleOrDefault(u => u.Id == 5);This program shows how to use First, FirstOrDefault, Single, and SingleOrDefault on a list of numbers. It prints the results to the console.
using System; using System.Linq; using System.Collections.Generic; class Program { static void Main() { List<int> numbers = new() { 10, 20, 30, 40 }; int first = numbers.First(); int firstOrDefault = numbers.FirstOrDefault(); int single = numbers.Single(n => n == 20); int singleOrDefault = numbers.SingleOrDefault(n => n == 100); Console.WriteLine($"First: {first}"); Console.WriteLine($"FirstOrDefault: {firstOrDefault}"); Console.WriteLine($"Single (20): {single}"); Console.WriteLine($"SingleOrDefault (100): {singleOrDefault}"); } }
Use First() when you want the first item and expect the list not to be empty.
Use FirstOrDefault() to avoid errors if the list might be empty; it returns default value (like 0 or null).
Single() is strict: it throws if there is not exactly one matching item.
SingleOrDefault() returns default if no match, but throws if more than one match.
First() gets the first item or throws if none.
FirstOrDefault() gets the first item or default if none.
Single() gets the only matching item or throws if zero or many.
SingleOrDefault() gets the only matching item or default if none, throws if many.
Practice
Which method will throw an exception if the collection does not have exactly one matching element?
First(), FirstOrDefault(), Single(), SingleOrDefault()Solution
Step 1: Understand Single() behavior
Single() expects exactly one matching element and throws if there are zero or more than one.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.Final Answer:
Single() -> Option DQuick Check:
Throws on multiple matches = Single() [OK]
- Confusing Single() with First() which returns first match
- Thinking FirstOrDefault() throws on multiple matches
- Assuming SingleOrDefault() never throws
Which of the following is the correct syntax to get the first element or default from a list numbers?
var result = numbers._____();
Solution
Step 1: Identify method for first or default
FirstOrDefault() returns the first element or default if none found.Step 2: Check syntax correctness
numbers.FirstOrDefault() is valid syntax to get first or default.Final Answer:
FirstOrDefault -> Option BQuick Check:
FirstOrDefault() syntax correct for first or default [OK]
- Using Single() which throws if multiple elements
- Using First() which throws if empty
- Using SingleOrDefault() which expects single element
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);Solution
Step 1: Understand SingleOrDefault with predicate
SingleOrDefault returns the only element matching predicate or default if none, throws if multiple.Step 2: Check list and predicate
List has one element equal to 10, so SingleOrDefault returns 10.Final Answer:
10 -> Option AQuick Check:
SingleOrDefault returns single matching element = 10 [OK]
- Expecting 0 as default when element exists
- Thinking it throws because of multiple elements in list
- Confusing with FirstOrDefault behavior
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);Solution
Step 1: Analyze Single() with multiple matches
Single() throws InvalidOperationException if more than one element matches the predicate.Step 2: Check list contents
List has two "apple" strings, so Single() throws exception.Final Answer:
Throws InvalidOperationException due to multiple matches -> Option CQuick Check:
Single() throws on multiple matches [OK]
- Expecting Single() to return first match
- Thinking it returns null on duplicates
- Assuming syntax error in lambda
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?
Solution
Step 1: Understand requirements
We want the only user named "admin" or null if none, but avoid exceptions if multiple exist.Step 2: Evaluate methods
SingleOrDefault() throws if multiple matches, so risky. FirstOrDefault() returns first or null safely even if multiple exist.Final Answer:
FirstOrDefault() -> Option AQuick Check:
FirstOrDefault() safely returns first or null without exceptions [OK]
- Using SingleOrDefault() which throws on multiple matches
- Using Single() which throws on zero or multiple matches
- Using First() which throws if none found
