Enumerable.Last Metoda

Definicja

Zwraca ostatni element sekwencji.

Przeciążenia

Last<TSource>(IEnumerable<TSource>)

Zwraca ostatni element sekwencji.

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

Zwraca ostatni element sekwencji, który spełnia określony warunek.

Last<TSource>(IEnumerable<TSource>)

Zwraca ostatni element sekwencji.

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

Parametry typu

TSource

Typ elementów elementu source.

Parametry

source
IEnumerable<TSource>

Element , IEnumerable<T> który ma zwrócić ostatni element.

Zwraca

TSource

Wartość na ostatniej pozycji w sekwencji źródłowej.

Wyjątki

source to null.

Sekwencja źródłowa jest pusta.

Przykłady

W poniższym przykładzie kodu pokazano, jak użyć Last<TSource>(IEnumerable<TSource>) polecenia , aby zwrócić ostatni element tablicy.

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

Uwagi

Metoda Last<TSource>(IEnumerable<TSource>) zgłasza wyjątek, jeśli source nie zawiera żadnych elementów. Aby zamiast tego zwrócić wartość domyślną, gdy sekwencja źródłowa jest pusta, użyj LastOrDefault metody .

Dotyczy

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

Zwraca ostatni element sekwencji, który spełnia określony warunek.

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

Parametry typu

TSource

Typ elementów elementu source.

Parametry

source
IEnumerable<TSource>

Element do IEnumerable<T> zwrócenia elementu z.

predicate
Func<TSource,Boolean>

Funkcja testowania każdego elementu na stanie.

Zwraca

TSource

Ostatni element w sekwencji, który przechodzi test w określonej funkcji predykatu.

Wyjątki

source lub predicate to null.

Żaden element nie spełnia warunku w elemecie predicate.

-lub-

Sekwencja źródłowa jest pusta.

Przykłady

W poniższym przykładzie kodu pokazano, jak użyć Last<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) polecenia , aby zwrócić ostatni element tablicy, który spełnia warunek.

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

Uwagi

Metoda Last<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) zgłasza wyjątek, jeśli w elemecie nie znaleziono sourcepasującego elementu . Aby zamiast tego zwrócić wartość domyślną, gdy nie znaleziono pasującego elementu, użyj LastOrDefault metody .

Dotyczy