IEnumerable<T>.FirstOrDefault Method

Overloads

FirstOrDefault

Returns the first element of the sequence, or a default value if the sequence contains no elements.

FirstOrDefault(T)

Returns the first element of the sequence, or the specified default value if the sequence contains no elements.

FirstOrDefault(Predicate<T>)

Returns the first element of the sequence that satisfies the specified condition or a default value if no such element is found.

FirstOrDefault(Predicate<T>, T)

Returns the first element of the sequence that satisfies the specified condition or the specified default value if no such element is found.

FirstOrDefault

Returns the first element of the sequence, or a default value if the sequence contains no elements.

Delphi
function FirstOrDefault: T

Returns

T

Default(T) if source is empty; otherwise, the first element in the sequence.

FirstOrDefault(T)

Returns the first element of the sequence, or the specified default value if the sequence contains no elements.

Delphi
function FirstOrDefault(const defaultValue: T): T

Parameters

defaultValue
T

The default value to return if the sequence is empty.

Returns

T

defaultValue if the sequence is empty; otherwise, the first element in the sequence.

FirstOrDefault(Predicate<T>)

Returns the first element of the sequence that satisfies the specified condition or a default value if no such element is found.

Delphi
function FirstOrDefault(const predicate: Predicate<T>): T

Parameters

predicate
Predicate<T>

A function to test each element for a condition.

Returns

T

Default(T) if source is empty or if no element passes the test specified by predicate ; otherwise, the first element in the sequence that passes the test specified by predicate .

Exceptions

predicate is nil .

FirstOrDefault(Predicate<T>, T)

Returns the first element of the sequence that satisfies the specified condition or the specified default value if no such element is found.

Delphi
function FirstOrDefault(const predicate: Predicate<T>; const defaultValue: T): T

Parameters

predicate
Predicate<T>

A function to test each element for a condition.

defaultValue
T

The default value to return if the sequence is empty or no element passes the condition.

Returns

T

defaultValue if the sequence is empty or if no element passes the test specified by predicate ; otherwise, the first element in the sequence that passes the test specified by predicate .

Exceptions

predicate is nil .