Enumerable.Last Yöntem

Tanım

Bir dizinin son öğesini döndürür.

Aşırı Yüklemeler

Last<TSource>(IEnumerable<TSource>)

Bir dizinin son öğesini döndürür.

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

Belirtilen koşulu karşılayan bir dizinin son öğesini döndürür.

Last<TSource>(IEnumerable<TSource>)

Bir dizinin son öğesini döndürür.

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

Tür Parametreleri

TSource

öğelerinin sourcetürü.

Parametreler

source
IEnumerable<TSource>

öğesinin IEnumerable<T> son öğesini döndürmek için.

Döndürülenler

TSource

Kaynak dizideki son konumdaki değer.

Özel durumlar

source, null değeridir.

Kaynak dizisi boş.

Örnekler

Aşağıdaki kod örneği, bir dizinin son öğesini döndürmek için nasıl kullanılacağını Last<TSource>(IEnumerable<TSource>) gösterir.

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

Açıklamalar

yöntemi Last<TSource>(IEnumerable<TSource>) , öğe içermiyorsa source bir özel durum oluşturur. Bunun yerine, kaynak dizisi boş olduğunda varsayılan bir değer döndürmek LastOrDefault için yöntemini kullanın.

Şunlara uygulanır

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

Belirtilen koşulu karşılayan bir dizinin son öğesini döndürür.

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

Tür Parametreleri

TSource

öğelerinin sourcetürü.

Parametreler

source
IEnumerable<TSource>

Öğesi IEnumerable<T> döndürülecek öğesi.

predicate
Func<TSource,Boolean>

Bir koşul için her öğeyi test etmek için bir işlev.

Döndürülenler

TSource

Belirtilen koşul işlevinde testi geçen dizideki son öğe.

Özel durumlar

source veya predicate şeklindedir null.

hiçbir öğe içindeki predicatekoşulu karşılar.

-veya-

Kaynak dizisi boş.

Örnekler

Aşağıdaki kod örneği, bir koşulu karşılayan bir dizinin son öğesini döndürmek için nasıl kullanılacağını Last<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) gösterir.

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

Açıklamalar

içinde Last<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) eşleşen bir öğe bulunamazsa sourceyöntemi bir özel durum oluşturur. Bunun yerine eşleşen öğe bulunamadığında varsayılan bir değer döndürmek LastOrDefault için yöntemini kullanın.

Şunlara uygulanır