Enumerable.First Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Returns the first element of a sequence.
Overloads
| Name | Description |
|---|---|
|
First |
Returns the first element of a sequence. |
|
First |
Returns the first element in a sequence that satisfies a specified condition. |
First(IEnumerable)
- Source:
- First.cs
- Source:
- First.cs
- Source:
- First.cs
- Source:
- First.cs
- Source:
- First.cs
Returns the first element of a sequence.
public:
generic
[System::Runtime::CompilerServices::Extension]
static TSource First(System::Collections::Generic::IEnumerable ^ source);
public static TSource First(this System.Collections.Generic.IEnumerable source);
static member First : seq<'Source> -> 'Source
Public Function First(Of TSource) (source As IEnumerable(Of TSource)) As TSource
Type Parameters
- TSource
The type of the elements of source.
Parameters
- source
-
IEnumerable
The IEnumerable
Returns
The first element in the specified sequence.
Exceptions
source is null.
The source sequence is empty.
Examples
The following code example demonstrates how to use First
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
Remarks
The Firstsource contains no elements. To instead return a default value when the source sequence is empty, use the FirstOrDefault method.
Applies to
First(IEnumerable, Func)
- Source:
- First.cs
- Source:
- First.cs
- Source:
- First.cs
- Source:
- First.cs
- Source:
- First.cs
Returns the first element in a sequence that satisfies a specified condition.
public:
generic
[System::Runtime::CompilerServices::Extension]
static TSource First(System::Collections::Generic::IEnumerable ^ source, Func ^ predicate);
public static TSource First(this System.Collections.Generic.IEnumerable source, Func predicate);
static member First : seq<'Source> * Func<'Source, bool> -> 'Source
Public Function First(Of TSource) (source As IEnumerable(Of TSource), predicate As Func(Of TSource, Boolean)) As TSource
Type Parameters
- TSource
The type of the elements of source.
Parameters
- source
-
IEnumerable
An IEnumerable
- predicate
-
Func
Boolean>
A function to test each element for a condition.
Returns
The first element in the sequence that passes the test in the specified predicate function.
Exceptions
source or predicate is null.
No element satisfies the condition in predicate.
-or-
The source sequence is empty.
Examples
The following code example demonstrates how to use First
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
Remarks
The Firstsource. To instead return a default value when no matching element is found, use the FirstOrDefault method.