Enumerable.First Yöntem

Tanım

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

Aşırı Yüklemeler

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

Belirtilen koşulu karşılayan bir dizideki ilk öğeyi döndürür.

First<TSource>(IEnumerable<TSource>)

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

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

Belirtilen koşulu karşılayan bir dizideki ilk öğeyi döndürür.

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static TSource First(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, bool> ^ predicate);
public static TSource First<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate);
static member First : seq<'Source> * Func<'Source, bool> -> 'Source
<Extension()>
Public Function First(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 ilk öğ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 ilk öğesini döndürmek için nasıl kullanılacağını First<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) gösterir.

int[] numbers = { 9, 34, 65, 92, 87, 435, 3, 54,
                    83, 23, 87, 435, 67, 12, 19 };

int first = numbers.First(number => number > 80);

Console.WriteLine(first);

/*
 This code produces the following output:

 92
*/
' Create an array of integers.
Dim numbers() As Integer =
{9, 34, 65, 92, 87, 435, 3, 54, 83, 23, 87, 435, 67, 12, 19}

' Select the first element in the array whose value is greater than 80.
Dim first As Integer = numbers.First(Function(number) number > 80)

' Display the output.
Console.WriteLine(first)

' This code produces the following output:
'
' 92

Açıklamalar

içinde First<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 FirstOrDefault için yöntemini kullanın.

Şunlara uygulanır

First<TSource>(IEnumerable<TSource>)

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

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static TSource First(System::Collections::Generic::IEnumerable<TSource> ^ source);
public static TSource First<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);
static member First : seq<'Source> -> 'Source
<Extension()>
Public Function First(Of TSource) (source As IEnumerable(Of TSource)) As TSource

Tür Parametreleri

TSource

öğelerinin sourcetürü.

Parametreler

source
IEnumerable<TSource>

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

Döndürülenler

TSource

Belirtilen dizideki ilk öğe.

Özel durumlar

source, null değeridir.

Kaynak dizisi boş.

Örnekler

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

int[] numbers = { 9, 34, 65, 92, 87, 435, 3, 54,
                    83, 23, 87, 435, 67, 12, 19 };

int first = numbers.First();

Console.WriteLine(first);

/*
 This code produces the following output:

 9
*/
' Create an array of integers.
Dim numbers() As Integer =
{9, 34, 65, 92, 87, 435, 3, 54, 83, 23, 87, 435, 67, 12, 19}

' Select the first element in the array.
Dim first As Integer = numbers.First()

' Display the output.
Console.WriteLine(first)

' This code produces the following output:
'
' 9

Açıklamalar

yöntemi First<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 FirstOrDefault için yöntemini kullanın.

Şunlara uygulanır