Enumerable.First メソッド

定義

シーケンスの最初の要素を返します。

オーバーロード

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

指定された述語関数でテストに合格する、シーケンスの最初の要素。

例外

source または predicatenull です。

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

注釈

First<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) 一致する要素が見つからない場合、メソッドは例外を sourceスローします。 一致する要素が見つからないときに既定値を返すには、 メソッドを使用します FirstOrDefault

適用対象