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>)

Źródło:
Last.cs
Źródło:
Last.cs
Źródło:
Last.cs

Zwraca ostatni element sekwencji.

C#
public static TSource Last<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);

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.

C#
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
*/

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

.NET 9 i inne wersje
Produkt Wersje
.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<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)

Źródło:
Last.cs
Źródło:
Last.cs
Źródło:
Last.cs

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

C#
public static TSource Last<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate);

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.

C#
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
*/

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

.NET 9 i inne wersje
Produkt Wersje
.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