1/23
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
First()
A LINQ element operator provided by Enumerable that returns the first element of a sequence and throws an InvalidOperationException if no matching element exists. Domain: C# → .NET → System.Linq → LINQ Element Retrieval
var result = numbers.First();
Returns the first element in the sequence and throws an InvalidOperationException if the sequence is empty.
var result = numbers.First(n => n > 10);
Returns the first element satisfying the specified condition and throws an InvalidOperationException if no matching element exists.
FirstOrDefault()
A LINQ element operator that returns the first element of a sequence, or the type's default value if no element exists.
var result = numbers.FirstOrDefault();
Returns the first number in the sequence, or the default value for the element type if the sequence is empty.
var result = numbers.FirstOrDefault(n => n > 10);
Returns the first element satisfying the condition, or the element type's default value if no matching element exists.
Last()
A LINQ element operator that returns the last element of a sequence and throws an InvalidOperationException if no matching element exists.
var result = numbers.Last();
Returns the last element of the sequence and throws an InvalidOperationException if the sequence is empty.
var result = numbers.Last(n => n < 100);
Returns the last element satisfying the specified condition and throws an InvalidOperationException if no matching element exists.
LastOrDefault()
A LINQ element operator that returns the last element of a sequence, or the type's default value if no element exists.
var result = numbers.LastOrDefault();
Returns the last element of the sequence, or the element type's default value if the sequence is empty.
var result = numbers.LastOrDefault(n => n < 100);
Returns the last element satisfying the condition, or the element type's default value if no matching element exists.
Single()
A LINQ element operator that requires exactly one matching element and throws an InvalidOperationException if zero or more than one matching element exists.
var result = numbers.Single();
Returns the sequence's only element and throws an InvalidOperationException if the sequence contains zero or more than one element.
var result = people.Single(p => p.Id == targetId);
Returns the only element satisfying the condition and throws an InvalidOperationException if zero or multiple elements satisfy it.
SingleOrDefault()
A LINQ element operator that returns the only matching element or the type's default value when no match exists, but still throws an InvalidOperationException when multiple matches exist.
var result = numbers.SingleOrDefault();
Returns the only element, returns the element type's default value if the sequence is empty, and throws an InvalidOperationException if multiple elements exist.
var result = people.SingleOrDefault(p => p.Id == targetId);
Returns the only matching element, returns the default value when no match exists, and throws an InvalidOperationException if multiple elements match.
ElementAt()
A LINQ element operator that returns the element at a specified zero-based position and throws an ArgumentOutOfRangeException when that position does not exist.
var result = numbers.ElementAt(3);
Returns the element at zero-based position 3, which is the fourth element in the sequence.
ElementAtOrDefault()
A LINQ element operator that returns the element at a specified zero-based position, or the element type's default value when that position does not exist.
var result = numbers.ElementAtOrDefault(10);
Returns the element at zero-based position 10, or the element type's default value if the sequence does not contain that position.
First() vs Single()
First() returns the first matching element even if additional matches exist, whereas Single() requires exactly one matching element and throws if multiple matches exist.
FirstOrDefault() vs SingleOrDefault()
FirstOrDefault() returns the first match and allows additional matches, whereas SingleOrDefault() requires at most one matching element and throws if multiple matches exist.