Enumerable.First 메서드
정의
시퀀스의 첫 번째 요소를 반환합니다.Returns the first element of a sequence.
오버로드
First<TSource>(IEnumerable<TSource>) |
시퀀스의 첫 번째 요소를 반환합니다.Returns the first element of a sequence. |
First<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) |
시퀀스에서 지정된 조건에 맞는 첫 번째 요소를 반환합니다.Returns the first element in a sequence that satisfies a specified condition. |
First<TSource>(IEnumerable<TSource>)
시퀀스의 첫 번째 요소를 반환합니다.Returns the first element of a sequence.
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
요소의 형식입니다.The type of the elements of source
.
매개 변수
- source
- IEnumerable<TSource>
첫 번째 요소를 반환할 IEnumerable<T>입니다.The IEnumerable<T> to return the first element of.
반환
- TSource
지정된 시퀀스의 첫 번째 요소입니다.The first element in the specified sequence.
예외
source
이(가) null
인 경우source
is null
.
소스 시퀀스가 비어 있는 경우The source sequence is empty.
예제
다음 코드 예제에서는를 사용 하 여 First<TSource>(IEnumerable<TSource>) 배열의 첫 번째 요소를 반환 하는 방법을 보여 줍니다.The following code example demonstrates how to use First<TSource>(IEnumerable<TSource>) to return the first element of an array.
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
설명
First<TSource>(IEnumerable<TSource>)에 요소가 없는 경우 메서드는 예외를 throw 합니다 source
.The First<TSource>(IEnumerable<TSource>) method throws an exception if source
contains no elements. 소스 시퀀스가 비어 있는 경우 기본값을 대신 반환 하려면 메서드를 사용 FirstOrDefault 합니다.To instead return a default value when the source sequence is empty, use the FirstOrDefault method.
적용 대상
First<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
시퀀스에서 지정된 조건에 맞는 첫 번째 요소를 반환합니다.Returns the first element in a sequence that satisfies a specified condition.
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
요소의 형식입니다.The type of the elements of source
.
매개 변수
- source
- IEnumerable<TSource>
요소를 반환할 IEnumerable<T>입니다.An IEnumerable<T> to return an element from.
각 요소를 조건에 대해 테스트하는 함수입니다.A function to test each element for a condition.
반환
- TSource
시퀀스에서 지정된 조건자 함수의 테스트를 통과하는 첫 번째 요소입니다.The first element in the sequence that passes the test in the specified predicate function.
예외
source
또는 predicate
가 null
인 경우source
or predicate
is null
.
predicate
의 조건을 충족하는 요소가 없는 경우No element satisfies the condition in predicate
.
또는-or- 소스 시퀀스가 비어 있는 경우The source sequence is empty.
예제
다음 코드 예제에서는를 사용 하 여 First<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) 조건을 충족 하는 배열의 첫 번째 요소를 반환 하는 방법을 보여 줍니다.The following code example demonstrates how to use First<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) to return the first element of an array that satisfies a condition.
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>)에서 일치 하는 요소를 찾을 수 없는 경우 메서드는 예외를 throw 합니다 source
.The First<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) method throws an exception if no matching element is found in source
. 일치 하는 요소가 없는 경우 기본값을 대신 반환 하려면 메서드를 사용 FirstOrDefault 합니다.To instead return a default value when no matching element is found, use the FirstOrDefault method.