0
0
C Sharp (C#)programming~10 mins

First, Single, and their OrDefault variants in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - First, Single, and their OrDefault variants
Start with a collection
Check if collection is empty?
Return default
Throw exception
This flow shows how First and Single methods check the collection: if empty, return default or throw; if multiple matches for Single, throw; otherwise return the element.
Execution Sample
C Sharp (C#)
var numbers = new[] {1, 2, 3};
var first = numbers.First();
var single = numbers.Single(x => x == 2);
var firstOrDefault = numbers.FirstOrDefault(x => x > 3);
var singleOrDefault = numbers.SingleOrDefault(x => x > 3);
This code gets the first element, a single matching element, and uses OrDefault variants to safely handle no matches.
Execution Table
StepMethod CallCondition CheckedResultAction Taken
1First()Collection not emptyTrueReturn first element: 1
2Single(x == 2)Exactly one element matchesTrueReturn element: 2
3FirstOrDefault(x > 3)Any element matches?FalseReturn default: 0
4SingleOrDefault(x > 3)Any element matches?FalseReturn default: 0
5Single(x > 1)More than one element matchesTrueThrow InvalidOperationException
6First()Collection emptyFalseThrow InvalidOperationException
7FirstOrDefault()Collection emptyTrueReturn default: 0
💡 Execution stops when method returns element, default, or throws exception.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
numbers[1, 2, 3][1, 2, 3][1, 2, 3][1, 2, 3][1, 2, 3]
firstundefined1111
singleundefinedundefined222
firstOrDefaultundefinedundefinedundefined00
singleOrDefaultundefinedundefinedundefinedundefined0
Key Moments - 3 Insights
Why does Single throw an exception when more than one element matches?
Single expects exactly one match. If multiple elements match, it throws to signal ambiguity, as shown in step 5 of the execution_table.
What is the difference between First and FirstOrDefault when the collection is empty?
First throws an exception if empty (step 6), while FirstOrDefault returns the default value (step 7), avoiding exceptions.
When does SingleOrDefault return default instead of throwing?
SingleOrDefault returns default if no elements match (step 4), but throws if multiple elements match, unlike FirstOrDefault.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what value does 'firstOrDefault' have after step 3?
A2
B0
C1
DException thrown
💡 Hint
Check the 'Result' and 'Action Taken' columns for step 3 in execution_table.
At which step does Single throw an exception due to multiple matches?
AStep 5
BStep 2
CStep 6
DStep 7
💡 Hint
Look for 'Throw InvalidOperationException' in the 'Action Taken' column.
If the collection was empty, what would First() do according to the execution_table?
AReturn default value
BReturn first element
CThrow InvalidOperationException
DReturn null
💡 Hint
See step 6 where First() is called on an empty collection.
Concept Snapshot
First() returns the first element or throws if empty.
Single() returns the only matching element or throws if none or multiple.
FirstOrDefault() returns first element or default if empty.
SingleOrDefault() returns single matching element or default if none, throws if multiple.
Use OrDefault variants to avoid exceptions on empty or no matches.
Full Transcript
This visual execution shows how First, Single, and their OrDefault variants work in C#. First returns the first element or throws if the collection is empty. Single returns the only matching element or throws if none or more than one match exists. The OrDefault variants return default values instead of throwing exceptions when no elements match or the collection is empty. The execution table traces each method call, conditions checked, and results or exceptions. Variable tracking shows how variables change after each step. Key moments clarify common confusions about exceptions and default returns. The quiz tests understanding of method behaviors at specific steps.