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 要素。

例外

sourcenullです。

index がシーケンスの source 範囲外です。

注釈

実装の型が実装されているsourceIList<T>場合、その実装は、指定したインデックス位置にある要素を取得するために使用されます。 それ以外の場合、このメソッドは指定した要素を取得します。

このメソッドは、範囲外の場合 index に例外をスローします。 指定したインデックスが範囲外の場合に既定値を返すには、メソッドを 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

ソース シーケンス内の指定された位置にある要素。

例外

sourcenullです。

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 に例外をスローします。 代わりに、指定したインデックスが範囲外の場合に既定値を返すには、メソッドを ElementAtOrDefault 使用します。

適用対象