Enumerable.First 方法

定義

傳回序列的第一個專案。

多載

名稱 Description
First<TSource>(IEnumerable<TSource>)

傳回序列的第一個專案。

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

傳回序列中符合指定條件的第一個專案。

First<TSource>(IEnumerable<TSource>)

傳回序列的第一個專案。

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的類型。

參數

source
IEnumerable<TSource>

IEnumerable<T>還原第一個元素。

傳回

TSource

指定序列中的第一個元素。

例外狀況

sourcenull

來源序列是空的。

範例

以下程式碼範例示範如何使用 First<TSource>(IEnumerable<TSource>) 回傳陣列的第一個元素。

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

備註

source 不包含元素,該First<TSource>(IEnumerable<TSource>)方法會拋出例外。 若想在來源序列為空時回傳預設值,請使用該 FirstOrDefault 方法。

適用於

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

傳回序列中符合指定條件的第一個專案。

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的類型。

參數

source
IEnumerable<TSource>

一個 IEnumerable<T> 還原元素的來源。

predicate
Func<TSource,Boolean>

一個用來測試每個元素條件的函數。

傳回

TSource

序列中第一個通過指定謂詞函數測試的元素。

例外狀況

sourcepredicatenull

沒有任何元素滿足該 predicate條件。

-或-

來源序列是空的。

範例

以下程式碼範例示範如何使用 First<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) 回傳滿足條件的陣列第一個元素。

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

備註

若在 中找不到source匹配元素,該First<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)方法會拋出例外。 若想在找不到匹配元素時回傳預設值,請使用該 FirstOrDefault 方法。

適用於