Enumerable.First 메서드

정의

시퀀스의 첫 번째 요소를 반환합니다.

오버로드

First<TSource>(IEnumerable<TSource>)

시퀀스의 첫 번째 요소를 반환합니다.

First<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)

시퀀스에서 지정된 조건에 맞는 첫 번째 요소를 반환합니다.

First<TSource>(IEnumerable<TSource>)

Source:
First.cs
Source:
First.cs
Source:
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>입니다.

반환

TSource

지정된 시퀀스의 첫 번째 요소입니다.

예외

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

설명

First<TSource>(IEnumerable<TSource>) 요소가 없으면 source 메서드가 예외를 throw합니다. 대신 소스 시퀀스가 비어 있을 때 기본값을 반환하려면 메서드를 FirstOrDefault 사용합니다.

적용 대상

First<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)

Source:
First.cs
Source:
First.cs
Source:
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>입니다.

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>) 에서 일치하는 요소가 없는 경우 예외를 sourcethrow합니다. 일치하는 요소를 찾을 수 없을 때 기본값을 반환하려면 메서드를 FirstOrDefault 사용합니다.

적용 대상