Enumerable.ElementAtOrDefault メソッド

定義

オーバーロード

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

シーケンス内の指定したインデックス位置にある要素を返します。インデックスが範囲外の場合は既定値を返します。

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

シーケンス内の指定したインデックス位置にある要素を返します。インデックスが範囲外の場合は既定値を返します。

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

シーケンス内の指定したインデックス位置にある要素を返します。インデックスが範囲外の場合は既定値を返します。

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

取得する要素のインデックス。これは、シーケンスの先頭または末尾のいずれかから取得されます。

戻り値

TSource

defaultがシーケンスの境界外にあるsource場合indexは 。それ以外の場合は、シーケンス内の指定した位置にあるsource要素。

例外

sourcenullです。

注釈

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

参照型と null 許容型の既定値は です null

適用対象

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

シーケンス内の指定したインデックス位置にある要素を返します。インデックスが範囲外の場合は既定値を返します。

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 から始まるインデックス。

戻り値

TSource

インデックスがソース シーケンスの範囲外の場合は default (TSource)。それ以外の場合は、ソース シーケンスの指定した位置にある要素。

例外

sourcenullです。

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>場合、その実装は、指定したインデックス位置の要素を取得するために使用されます。 それ以外の場合、このメソッドは指定した要素を取得します。

参照型と null 許容型の既定値は です null

適用対象