Enumerable.First メソッド
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
シーケンスの最初の要素を返します。
First<TSource>(IEnumerable<TSource>) |
シーケンスの最初の要素を返します。 |
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);
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>。
戻り値
指定されたシーケンスの最初の要素。
例外
source
が null
です。
ソース シーケンスが空です。
例
次のコード例では、 を使用 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 。
適用対象
.NET 9 およびその他のバージョン
製品 | バージョン |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1 |
UWP | 10.0 |
- ソース:
- 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>。
戻り値
指定された述語関数でテストに合格する、シーケンスの最初の要素。
例外
source
または predicate
が null
です。
例
次のコード例では、 を使用 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 。
適用対象
.NET 9 およびその他のバージョン
製品 | バージョン |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1 |
UWP | 10.0 |
.NET に関するフィードバック
.NET はオープンソース プロジェクトです。 フィードバックを提供するにはリンクを選択します。