Enumerable.LastOrDefault 方法

定义

返回序列中的最后一个元素;如果未找到该元素,则返回默认值。

重载

LastOrDefault<TSource>(IEnumerable<TSource>)

返回序列中的最后一个元素;如果序列中不包含任何元素,则返回默认值。

LastOrDefault<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)

返回序列中满足条件的最后一个元素;如果未找到这样的元素,则返回默认值。

LastOrDefault<TSource>(IEnumerable<TSource>, TSource)

返回序列的最后一个元素,如果序列不包含任何元素,则返回指定的默认值。

LastOrDefault<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>, TSource)

返回序列中满足条件的最后一个元素;如果未找到此类元素,则返回指定的默认值。

LastOrDefault<TSource>(IEnumerable<TSource>)

Source:
Last.cs
Source:
Last.cs
Source:
Last.cs

返回序列中的最后一个元素;如果序列中不包含任何元素,则返回默认值。

C#
public static TSource LastOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);
C#
public static TSource? LastOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

要返回其最后一个元素的 IEnumerable<T>

返回

TSource

如果源序列为空,则为 default(TSource);否则为 IEnumerable<T> 中的最后一个元素。

例外

sourcenull

示例

下面的代码示例演示如何对空数组使用 LastOrDefault<TSource>(IEnumerable<TSource>)

C#
string[] fruits = { };
string last = fruits.LastOrDefault();
Console.WriteLine(
    String.IsNullOrEmpty(last) ? "<string is null or empty>" : last);

/*
 This code produces the following output:

 <string is null or empty>
*/

有时,如果集合不包含任何元素,则 的值 default(TSource) 不是要使用的默认值。 可以使用 DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource) 方法指定在集合为空时要使用的默认值,而不是检查不需要的默认值的结果,然后根据需要对其进行更改。 然后,调用 Last<TSource>(IEnumerable<TSource>) 以获取最后一个元素。 如果月份的数字天数集合为空,下面的代码示例使用这两种方法来获取默认值 1。 由于整数的默认值为 0,这与月份中的任何一天都不对应,因此必须将默认值指定为 1。 执行完查询后,将检查第一个结果变量是否为不需要的默认值。 第二个结果变量通过使用 DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource) 将默认值指定为 1 来获取。

C#
List<int> daysOfMonth = new List<int> { };

// Setting the default value to 1 after the query.
int lastDay1 = daysOfMonth.LastOrDefault();
if (lastDay1 == 0)
{
    lastDay1 = 1;
}
Console.WriteLine("The value of the lastDay1 variable is {0}", lastDay1);

// Setting the default value to 1 by using DefaultIfEmpty() in the query.
int lastDay2 = daysOfMonth.DefaultIfEmpty(1).Last();
Console.WriteLine("The value of the lastDay2 variable is {0}", lastDay2);

/*
 This code produces the following output:

 The value of the lastDay1 variable is 1
 The value of the lastDay2 variable is 1
*/

注解

引用和可为空类型的默认值为 null

方法 LastOrDefault 不提供指定默认值的方法。 如果要指定 除 以外的 default(TSource)默认值, DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource) 请使用 示例部分所述的 方法。

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

LastOrDefault<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)

Source:
Last.cs
Source:
Last.cs
Source:
Last.cs

返回序列中满足条件的最后一个元素;如果未找到这样的元素,则返回默认值。

C#
public static TSource LastOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate);
C#
public static TSource? LastOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate);

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

要从中返回元素的 IEnumerable<T>

predicate
Func<TSource,Boolean>

用于测试每个元素是否满足条件的函数。

返回

TSource

如果序列为空或没有元素通过谓词函数中的测试,则为 default(TSource);否则,为通过谓词函数中的测试的最后一个元素。

例外

sourcepredicatenull

示例

下面的代码示例演示如何通过传入谓词来使用 LastOrDefault<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) 。 在对 方法的第二次调用中,序列中没有满足条件的元素。

C#
double[] numbers = { 49.6, 52.3, 51.0, 49.4, 50.2, 48.3 };

double last50 = numbers.LastOrDefault(n => Math.Round(n) == 50.0);

Console.WriteLine("The last number that rounds to 50 is {0}.", last50);

double last40 = numbers.LastOrDefault(n => Math.Round(n) == 40.0);

Console.WriteLine(
    "The last number that rounds to 40 is {0}.",
    last40 == 0.0 ? "<DOES NOT EXIST>" : last40.ToString());

/*
 This code produces the following output:

 The last number that rounds to 50 is 50.2.
 The last number that rounds to 40 is <DOES NOT EXIST>.
*/

注解

引用和可为空类型的默认值为 null

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

LastOrDefault<TSource>(IEnumerable<TSource>, TSource)

Source:
Last.cs
Source:
Last.cs
Source:
Last.cs

返回序列的最后一个元素,如果序列不包含任何元素,则返回指定的默认值。

C#
public static TSource LastOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, TSource defaultValue);

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

要返回其最后一个元素的 IEnumerable<T>

defaultValue
TSource

如果序列为空,则返回的默认值。

返回

TSource

defaultValue 如果源序列为空,则为 ;否则为 中的最后一个 IEnumerable<T>元素。

例外

sourcenull

适用于

.NET 9 和其他版本
产品 版本
.NET 6, 7, 8, 9

LastOrDefault<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>, TSource)

Source:
Last.cs
Source:
Last.cs
Source:
Last.cs

返回序列中满足条件的最后一个元素;如果未找到此类元素,则返回指定的默认值。

C#
public static TSource LastOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate, TSource defaultValue);

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

要从中返回元素的 IEnumerable<T>

predicate
Func<TSource,Boolean>

用于测试每个元素是否满足条件的函数。

defaultValue
TSource

如果序列为空,则返回的默认值。

返回

TSource

defaultValue 如果序列为空,或者没有元素通过谓词函数中的测试,则为 ;否则为谓词函数中通过测试的最后一个元素。

例外

sourcepredicatenull

适用于

.NET 9 和其他版本
产品 版本
.NET 6, 7, 8, 9