FirstOrDefault
Method
Returns the first element of a sequence, or a default value if the sequence contains no elements.
FirstOrDefault
<TSource>(this IEnumerable<TSource> source);
FirstOrDefault
<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
Parameters
-
source
:An IEnumerable<TSource> to return the first element of.
-
predicate
:A function to test each element for a condition.
Return Value
TSource
: The first element in the specified sequence that passes the test in the specified predicate, or default (null
for reference types, zero for numeric types, false
for bool, etc.) if no element passes the test.
Remarks
The FirstOrDefault
method returns the first element of a sequence. If the sequence is empty, it returns the default value for the type TSource
. If a predicate is provided, it returns the first element that satisfies the predicate, or the default value if no element satisfies the predicate.
The default value for reference types is null
, for numeric types it is 0, and for boolean types it is false
.
This method has no effect on the current instance and does not return a modified sequence.
The behavior of FirstOrDefault
is different from First
. First
throws an exception if the sequence is empty or no element satisfies the predicate, whereas FirstOrDefault
returns the default value.
Examples
Example 1: Basic Usage
Retrieving the first element of a sequence or a default value if the sequence is empty.
using System;
using System.Collections.Generic;
using System.Linq;
public class Example
{
public static void Main(string[] args)
{
List<int> numbers1 = new List<int> { 5, 10, 15, 20 };
int firstOrDefaultNumber1 = numbers1.FirstOrDefault();
Console.WriteLine($"First or default element: {firstOrDefaultNumber1}"); // Output: 5
List<int> numbers2 = new List<int>();
int firstOrDefaultNumber2 = numbers2.FirstOrDefault();
Console.WriteLine($"First or default element (empty list): {firstOrDefaultNumber2}"); // Output: 0
List<string> names1 = new List<string> { "Alice", "Bob", "Charlie" };
string firstOrDefaultName1 = names1.FirstOrDefault();
Console.WriteLine($"First or default element: {firstOrDefaultName1 ?? "null"}"); // Output: Alice
List<string> names2 = new List<string>();
string firstOrDefaultName2 = names2.FirstOrDefault();
Console.WriteLine($"First or default element (empty list): {firstOrDefaultName2 ?? "null"}"); // Output: null
}
}
Example 2: With Predicate
Retrieving the first element that satisfies a condition, or a default value if no such element exists.
using System;
using System.Collections.Generic;
using System.Linq;
public class Example
{
public static void Main(string[] args)
{
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6 };
// Find the first number greater than 3
int firstGreaterThan3 = numbers.FirstOrDefault(n => n > 3);
Console.WriteLine($"First number greater than 3: {firstGreaterThan3}"); // Output: 4
// Find the first number greater than 10 (none exist)
int firstGreaterThan10 = numbers.FirstOrDefault(n => n > 10);
Console.WriteLine($"First number greater than 10: {firstGreaterThan10}"); // Output: 0
List<string> words = new List<string> { "apple", "banana", "cherry" };
// Find the first word starting with 'b'
string firstStartingWithB = words.FirstOrDefault(w => w.StartsWith("b"));
Console.WriteLine($"First word starting with 'b': {firstStartingWithB ?? "null"}"); // Output: banana
// Find the first word starting with 'z' (none exist)
string firstStartingWithZ = words.FirstOrDefault(w => w.StartsWith("z"));
Console.WriteLine($"First word starting with 'z': {firstStartingWithZ ?? "null"}"); // Output: null
}
}