Enumerable.Last メソッド
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
シーケンスの最後の要素を返します。
Last<TSource>(IEnumerable<TSource>) |
シーケンスの最後の要素を返します。 |
Last<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) |
指定された条件を満たす、シーケンスの最後の要素を返します。 |
- ソース:
- Last.cs
- ソース:
- Last.cs
- ソース:
- Last.cs
シーケンスの最後の要素を返します。
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
static TSource Last(System::Collections::Generic::IEnumerable<TSource> ^ source);
public static TSource Last<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);
static member Last : seq<'Source> -> 'Source
<Extension()>
Public Function Last(Of TSource) (source As IEnumerable(Of TSource)) As TSource
型パラメーター
- TSource
source
の要素の型。
パラメーター
- source
- IEnumerable<TSource>
最後の要素を返す IEnumerable<T>。
戻り値
ソース シーケンスの最後の位置にある値。
例外
source
が null
です。
ソース シーケンスが空です。
例
次のコード例では、 を使用 Last<TSource>(IEnumerable<TSource>) して配列の最後の要素を返す方法を示します。
int[] numbers = { 9, 34, 65, 92, 87, 435, 3, 54,
83, 23, 87, 67, 12, 19 };
int last = numbers.Last();
Console.WriteLine(last);
/*
This code produces the following output:
19
*/
' Create an array of integers.
Dim numbers() As Integer =
{9, 34, 65, 92, 87, 435, 3, 54, 83, 23, 87, 67, 12, 19}
' Get the last item in the array.
Dim last As Integer = numbers.Last()
' Display the result.
Console.WriteLine(last)
' This code produces the following output:
'
' 19
注釈
要素が含まれない場合source
、メソッドはLast<TSource>(IEnumerable<TSource>)例外をスローします。 ソース シーケンスが空のときに既定値を返すには、 メソッドを使用します LastOrDefault 。
適用対象
.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 |
- ソース:
- Last.cs
- ソース:
- Last.cs
- ソース:
- Last.cs
指定された条件を満たす、シーケンスの最後の要素を返します。
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
static TSource Last(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, bool> ^ predicate);
public static TSource Last<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate);
static member Last : seq<'Source> * Func<'Source, bool> -> 'Source
<Extension()>
Public Function Last(Of TSource) (source As IEnumerable(Of TSource), predicate As Func(Of TSource, Boolean)) As TSource
型パラメーター
- TSource
source
の要素の型。
パラメーター
- source
- IEnumerable<TSource>
返される要素が含まれる IEnumerable<T>。
戻り値
指定された述語関数でテストに合格する、シーケンスの最後の要素。
例外
source
または predicate
が null
です。
例
次のコード例では、 を使用 Last<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) して、条件を満たす配列の最後の要素を返す方法を示します。
int[] numbers = { 9, 34, 65, 92, 87, 435, 3, 54,
83, 23, 87, 67, 12, 19 };
int last = numbers.Last(num => num > 80);
Console.WriteLine(last);
/*
This code produces the following output:
87
*/
' Create an array of integers.
Dim numbers() As Integer =
{9, 34, 65, 92, 87, 435, 3, 54, 83, 23, 87, 67, 12, 19}
' Get the last element in the array whose value is
' greater than 80.
Dim last As Integer = numbers.Last(Function(num) num > 80)
' Display the result.
Console.WriteLine(last)
' This code produces the following output:
'
' 87
注釈
で Last<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) 一致する要素が見つからない場合、メソッドは例外を source
スローします。 一致する要素が見つからないときに既定値を返すには、 メソッドを使用します LastOrDefault 。
適用対象
.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 |
.NET に関するフィードバック
.NET はオープンソース プロジェクトです。 フィードバックを提供するにはリンクを選択します。