Enumerable.ElementAtOrDefault 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
오버로드
| ElementAtOrDefault<TSource>(IEnumerable<TSource>, Index) |
시퀀스에서 지정된 인덱스의 요소를 반환하거나, 인덱스가 범위를 벗어나면 기본 값을 반환합니다. |
| ElementAtOrDefault<TSource>(IEnumerable<TSource>, Int32) |
시퀀스에서 지정된 인덱스의 요소를 반환하거나, 인덱스가 범위를 벗어나면 기본 값을 반환합니다. |
ElementAtOrDefault<TSource>(IEnumerable<TSource>, Index)
- Source:
- ElementAt.cs
- Source:
- ElementAt.cs
- Source:
- ElementAt.cs
시퀀스에서 지정된 인덱스의 요소를 반환하거나, 인덱스가 범위를 벗어나면 기본 값을 반환합니다.
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
static TSource ElementAtOrDefault(System::Collections::Generic::IEnumerable<TSource> ^ source, Index index);
public static TSource? ElementAtOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Index index);
static member ElementAtOrDefault : seq<'Source> * Index -> 'Source
<Extension()>
Public Function ElementAtOrDefault(Of TSource) (source As IEnumerable(Of TSource), index As Index) As TSource
형식 매개 변수
- TSource
source 요소의 형식입니다.
매개 변수
- source
- IEnumerable<TSource>
요소를 반환할 IEnumerable<T>입니다.
- index
- Index
시퀀스의 시작 또는 끝에서 검색할 요소의 인덱스입니다.
반환
default가 시퀀스의 source 범위를 벗어나면 index 이고, 그렇지 않으면 시퀀스에서 지정된 위치에 있는 source 요소입니다.
예외
source은 null입니다.
설명
의 source 형식이 를 구현하는 IList<T>경우 해당 구현은 지정된 인덱스에서 요소를 가져오는 데 사용됩니다. 그렇지 않으면 이 메서드는 지정된 요소를 가져옵니다.
참조 및 nullable 형식의 기본값은 입니다 null.
적용 대상
ElementAtOrDefault<TSource>(IEnumerable<TSource>, Int32)
- Source:
- ElementAt.cs
- Source:
- ElementAt.cs
- Source:
- ElementAt.cs
시퀀스에서 지정된 인덱스의 요소를 반환하거나, 인덱스가 범위를 벗어나면 기본 값을 반환합니다.
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
static TSource ElementAtOrDefault(System::Collections::Generic::IEnumerable<TSource> ^ source, int index);
public static TSource ElementAtOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, int index);
public static TSource? ElementAtOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, int index);
static member ElementAtOrDefault : seq<'Source> * int -> 'Source
<Extension()>
Public Function ElementAtOrDefault(Of TSource) (source As IEnumerable(Of TSource), index As Integer) As TSource
형식 매개 변수
- TSource
source 요소의 형식입니다.
매개 변수
- source
- IEnumerable<TSource>
요소를 반환할 IEnumerable<T>입니다.
- index
- Int32
검색할 요소의 0부터 시작하는 인덱스입니다.
반환
인덱스가 소스 시퀀스의 범위를 벗어나면 default(TSource)이고, 그렇지 않으면 소스 시퀀스에서 지정된 위치에 있는 요소입니다.
예외
source이(가) null인 경우
예제
다음 코드 예제에서는 ElementAtOrDefault을 사용하는 방법을 보여 줍니다. 이 예제에서는 배열의 범위를 벗어난 인덱스를 사용합니다.
string[] names =
{ "Hartono, Tommy", "Adams, Terry", "Andersen, Henriette Thaulow",
"Hedlund, Magnus", "Ito, Shu" };
int index = 20;
string name = names.ElementAtOrDefault(index);
Console.WriteLine(
"The name chosen at index {0} is '{1}'.",
index,
String.IsNullOrEmpty(name) ? "<no name at this index>" : name);
/*
This code produces the following output:
The name chosen at index 20 is '<no name at this index>'.
*/
' Create an array of strings.
Dim names() As String =
{"Hartono, Tommy", "Adams, Terry", "Andersen, Henriette Thaulow", "Hedlund, Magnus", "Ito, Shu"}
Dim index As Integer = 20
' Get a string at an index that is out of range in the array.
Dim name As String = names.ElementAtOrDefault(index)
Dim text As String = If(String.IsNullOrEmpty(name), "[THERE IS NO NAME AT THIS INDEX]", name)
' Display the output.
Console.WriteLine($"The name chosen at index {index} is {text}")
' This code produces the following output:
'
' The name chosen at index 20 is [THERE IS NO NAME AT THIS INDEX]
설명
의 source 형식이 를 구현하는 IList<T>경우 해당 구현은 지정된 인덱스에서 요소를 가져오는 데 사용됩니다. 그렇지 않으면 이 메서드는 지정된 요소를 가져옵니다.
참조 및 nullable 형식의 기본값은 입니다 null.