Enumerable.First Enumerable.First Enumerable.First Enumerable.First Method

定義

傳回序列的第一個項目。Returns the first element of a sequence.

多載

First<TSource>(IEnumerable<TSource>) First<TSource>(IEnumerable<TSource>) First<TSource>(IEnumerable<TSource>) First<TSource>(IEnumerable<TSource>)

傳回序列的第一個項目。Returns the first element of a sequence.

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

傳回序列中符合指定條件的第一個元素。Returns the first element in a sequence that satisfies a specified condition.

First<TSource>(IEnumerable<TSource>) First<TSource>(IEnumerable<TSource>) First<TSource>(IEnumerable<TSource>) First<TSource>(IEnumerable<TSource>)

傳回序列的第一個項目。Returns the first element of a sequence.

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

類型參數

TSource

source 項目的類型。The type of the elements of source.

參數

source
IEnumerable<TSource>

IEnumerable<T>来傳回的第一個元素。The IEnumerable<T> to return the first element of.

傳回

TSource TSource TSource TSource

指定之序列中的第一個項目。The first element in the specified sequence.

例外狀況

範例

下列程式碼範例示範如何使用First<TSource>(IEnumerable<TSource>)傳回陣列的第一個元素。The following code example demonstrates how to use First<TSource>(IEnumerable<TSource>) to return the first element of an array.

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.
MsgBox(first)

' This code produces the following output:
'
' 9

備註

First<TSource>(IEnumerable<TSource>)方法時擲回例外狀況source包含任何項目。The First<TSource>(IEnumerable<TSource>) method throws an exception if source contains no elements. 若要改為傳回預設值,當來源序列是空的使用FirstOrDefault方法。To instead return a default value when the source sequence is empty, use the FirstOrDefault method.

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

傳回序列中符合指定條件的第一個元素。Returns the first element in a sequence that satisfies a specified condition.

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

類型參數

TSource

source 項目的類型。The type of the elements of source.

參數

source
IEnumerable<TSource>

IEnumerable<T>来傳回的項目。An IEnumerable<T> to return an element from.

predicate
Func<TSource,Boolean>

用來測試每個項目是否符合條件的函式。A function to test each element for a condition.

傳回

TSource TSource TSource TSource

序列中通過指定之述詞函式所做測試的第一個項目。The first element in the sequence that passes the test in the specified predicate function.

例外狀況

sourcepredicatenullsource or predicate is null.

沒有任何項目符合 predicate 的條件。No element satisfies the condition in predicate.

-或--or- 來源序列是空的。The source sequence is empty.

範例

下列程式碼範例示範如何使用First<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)傳回陣列,符合條件的第一個元素。The following code example demonstrates how to use First<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) to return the first element of an array that satisfies a condition.

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.
MsgBox(first)

' This code produces the following output:
'
' 92

備註

First<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)方法擲回例外狀況,如果沒有相符的項目位於sourceThe First<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) method throws an exception if no matching element is found in source. 若要找到任何相符的項目時,請改為傳回預設值,請使用FirstOrDefault方法。To instead return a default value when no matching element is found, use the FirstOrDefault method.

適用於