Enumerable.First 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
시퀀스의 첫 번째 요소를 반환합니다.
오버로드
| Name | Description |
|---|---|
| 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> 첫 번째 요소를 반환할 값입니다.
반품
지정된 시퀀스의 첫 번째 요소입니다.
예외
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>) 예외를 throw합니다. 원본 시퀀스가 비어 있을 때 기본값을 대신 반환하려면 메서드를 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> 요소를 반환할 값입니다.
반품
지정된 조건자 함수에서 테스트를 통과하는 시퀀스의 첫 번째 요소입니다.
예외
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>) 일치하는 요소가 없는 경우 메서드는 예외를 sourcethrow합니다. 일치하는 요소를 찾을 수 없을 때 기본값을 반환하려면 메서드를 FirstOrDefault 사용합니다.