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 如果 index 位於序列界限 source 之外,則為 ,否則為序列中指定位置的專案 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

要擷取的項目之以零為起始索引。

傳回

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

適用於