Enumerable.ElementAt 메서드

정의

오버로드

ElementAt<TSource>(IEnumerable<TSource>, Index)

시퀀스에서 지정된 인덱스의 요소를 반환합니다.

ElementAt<TSource>(IEnumerable<TSource>, Int32)

시퀀스에서 지정된 인덱스의 요소를 반환합니다.

ElementAt<TSource>(IEnumerable<TSource>, Index)

시퀀스에서 지정된 인덱스의 요소를 반환합니다.

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static TSource ElementAt(System::Collections::Generic::IEnumerable<TSource> ^ source, Index index);
public static TSource ElementAt<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Index index);
static member ElementAt : seq<'Source> * Index -> 'Source
<Extension()>
Public Function ElementAt(Of TSource) (source As IEnumerable(Of TSource), index As Index) As TSource

형식 매개 변수

TSource

source 요소의 형식입니다.

매개 변수

source
IEnumerable<TSource>

요소를 반환할 IEnumerable<T>입니다.

index
Index

시퀀스의 시작 또는 끝에서 검색할 요소의 인덱스입니다.

반환

TSource

시퀀스의 지정된 위치에 있는 요소입니다 source .

예외

source이(가) null인 경우

index 가 시퀀스의 범위를 벗어났습니다 source .

설명

구현 IList<T>형식인 source 경우 해당 구현은 지정된 인덱스에서 요소를 가져오는 데 사용됩니다. 그렇지 않으면 이 메서드는 지정된 요소를 가져옵니다.

이 메서드는 범위를 벗어난 경우 index 예외를 throw합니다. 지정된 인덱스가 범위를 벗어날 때 기본값을 반환하려면 메서드를 ElementAtOrDefault 사용합니다.

적용 대상

ElementAt<TSource>(IEnumerable<TSource>, Int32)

시퀀스에서 지정된 인덱스의 요소를 반환합니다.

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static TSource ElementAt(System::Collections::Generic::IEnumerable<TSource> ^ source, int index);
public static TSource ElementAt<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, int index);
static member ElementAt : seq<'Source> * int -> 'Source
<Extension()>
Public Function ElementAt(Of TSource) (source As IEnumerable(Of TSource), index As Integer) As TSource

형식 매개 변수

TSource

source 요소의 형식입니다.

매개 변수

source
IEnumerable<TSource>

요소를 반환할 IEnumerable<T>입니다.

index
Int32

검색할 요소의 0부터 시작하는 인덱스입니다.

반환

TSource

소스 시퀀스에서 지정된 위치의 요소입니다.

예외

source이(가) null인 경우

index가 0보다 작거나 source의 요소 수보다 크거나 같은 경우

예제

다음 코드 예제에서는 특정 위치에서 요소를 반환하는 데 사용하는 ElementAt 방법을 보여 줍니다.

string[] names =
    { "Hartono, Tommy", "Adams, Terry", "Andersen, Henriette Thaulow",

        "Hedlund, Magnus", "Ito, Shu" };
Random random = new Random(DateTime.Now.Millisecond);

string name = names.ElementAt(random.Next(0, names.Length));

Console.WriteLine("The name chosen at random is '{0}'.", name);

/*
 This code produces output similar to the following:

 The name chosen at random is 'Ito, Shu'.
*/
' Create an array of strings.
Dim names() As String =
{"Hartono, Tommy", "Adams, Terry", "Andersen, Henriette Thaulow", "Hedlund, Magnus", "Ito, Shu"}

Dim random As Random = New Random(DateTime.Now.Millisecond)

' Get a string at a random index within the array.
Dim name As String = names.ElementAt(random.Next(0, names.Length))

' Display the output.
Console.WriteLine($"The name chosen at random is {name}")

' This code produces output similar to the following:
'
' The name chosen at random is Ito, Shu

설명

구현 IList<T>형식 source 인 경우 해당 구현은 지정된 인덱스에서 요소를 가져오는 데 사용됩니다. 그렇지 않으면 이 메서드는 지정된 요소를 가져옵니다.

이 메서드는 범위를 벗어난 경우 index 예외를 throw합니다. 지정한 인덱스가 범위를 벗어났을 때 기본값을 반환하려면 메서드를 ElementAtOrDefault 사용합니다.

적용 대상