Enumerable.Last 方法
定義
傳回序列的最後一個項目。Returns the last element of a sequence.
多載
Last<TSource>(IEnumerable<TSource>) |
傳回序列的最後一個項目。Returns the last element of a sequence. |
Last<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) |
傳回序列中符合指定之條件的最後一個元素。Returns the last element of a sequence that satisfies a specified condition. |
Last<TSource>(IEnumerable<TSource>)
傳回序列的最後一個項目。Returns the last element of a sequence.
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
項目的類型。The type of the elements of source
.
參數
- source
- IEnumerable<TSource>
要傳回最後一個項目的 IEnumerable<T>。An IEnumerable<T> to return the last element of.
傳回
- TSource
位於來源序列中最後一個位置的值。The value at the last position in the source sequence.
例外狀況
source
為 null
。source
is null
.
來源序列為空。The source sequence is empty.
範例
下列程式碼範例示範如何使用傳回 Last<TSource>(IEnumerable<TSource>) 陣列的最後一個元素。The following code example demonstrates how to use Last<TSource>(IEnumerable<TSource>) to return the last element of an array.
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
備註
Last<TSource>(IEnumerable<TSource>)如果 source
沒有包含任何元素,方法會擲回例外狀況。The Last<TSource>(IEnumerable<TSource>) method throws an exception if source
contains no elements. 當來源序列是空的時,若要改為傳回預設值,請使用 LastOrDefault 方法。To instead return a default value when the source sequence is empty, use the LastOrDefault method.
適用於
Last<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
傳回序列中符合指定之條件的最後一個元素。Returns the last element of a sequence that satisfies a specified condition.
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
項目的類型。The type of the elements of source
.
參數
- source
- IEnumerable<TSource>
傳回項目的 IEnumerable<T>。An IEnumerable<T> to return an element from.
傳回
- TSource
序列中通過指定之述詞函式所做測試的最後一個項目。The last element in the sequence that passes the test in the specified predicate function.
例外狀況
source
或 predicate
為 null
。source
or predicate
is null
.
沒有任何項目符合 predicate
的條件。No element satisfies the condition in predicate
.
-或--or- 來源序列為空。The source sequence is empty.
範例
下列程式碼範例示範如何使用傳回 Last<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) 陣列中滿足條件的最後一個元素。The following code example demonstrates how to use Last<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) to return the last element of an array that satisfies a condition.
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
。The Last<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) method throws an exception if no matching element is found in source
. 若要改為在找不到相符的元素時傳回預設值,請使用 LastOrDefault 方法。To instead return a default value when no matching element is found, use the LastOrDefault method.