Enumerable.First 方法

定義

傳回序列的第一個項目。

多載

First<TSource>(IEnumerable<TSource>)

傳回序列的第一個項目。

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

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

First<TSource>(IEnumerable<TSource>)

來源:
First.cs
來源:
First.cs
來源:
First.cs

傳回序列的第一個項目。

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

來源:
First.cs
來源:
First.cs
來源:
First.cs

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

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 方法。

適用於