Enumerable.Last メソッド

定義

シーケンスの最後の要素を返します。

オーバーロード

Last<TSource>(IEnumerable<TSource>)

シーケンスの最後の要素を返します。

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

指定された条件を満たす、シーケンスの最後の要素を返します。

Last<TSource>(IEnumerable<TSource>)

シーケンスの最後の要素を返します。

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>

戻り値

TSource

ソース シーケンスの最後の位置にある値。

例外

sourcenullです。

ソース シーケンスが空です。

次のコード例は、配列の最後の要素を返すために使用 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 使用します。

適用対象

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

指定された条件を満たす、シーケンスの最後の要素を返します。

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>

predicate
Func<TSource,Boolean>

各要素が条件を満たしているかどうかをテストする関数。

戻り値

TSource

指定された述語関数でテストに合格する、シーケンスの最後の要素。

例外

source または predicatenull です。

predicate の条件を満たす要素はありません。

  • または -

ソース シーケンスが空です。

次のコード例は、条件を満たす配列の最後の要素を返すために使用 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

適用対象