Enumerable.Last 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
傳回序列的最後一個專案。
多載
| 名稱 | Description |
|---|---|
| Last<TSource>(IEnumerable<TSource>) |
傳回序列的最後一個專案。 |
| Last<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) |
傳回符合指定條件之序列的最後一個專案。 |
Last<TSource>(IEnumerable<TSource>)
- 來源:
- Last.cs
- 來源:
- Last.cs
- 來源:
- 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
備註
若 Last<TSource>(IEnumerable<TSource>) 不包含元素,該source方法會拋出例外。 若想在來源序列為空時回傳預設值,請使用該 LastOrDefault 方法。
適用於
Last<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
- 來源:
- Last.cs
- 來源:
- Last.cs
- 來源:
- 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 方法。