Enumerable.ElementAtOrDefault Metoda

Definicja

Przeciążenia

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

Zwraca element w określonym indeksie w sekwencji lub wartość domyślną, jeśli indeks jest poza zakresem.

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

Zwraca element w określonym indeksie w sekwencji lub wartość domyślną, jeśli indeks jest poza zakresem.

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

Zwraca element w określonym indeksie w sekwencji lub wartość domyślną, jeśli indeks jest poza zakresem.

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

Parametry typu

TSource

Typ elementów elementu source.

Parametry

source
IEnumerable<TSource>

Element IEnumerable<T> do zwrócenia elementu z.

index
Index

Indeks elementu do pobrania, który jest od początku lub na końcu sekwencji.

Zwraca

TSource

default jeśli index znajduje się poza granicami source sekwencji; w przeciwnym razie element w określonej pozycji w source sekwencji.

Wyjątki

source to null.

Uwagi

Jeśli typ source implementuje IList<T>, ta implementacja jest używana do uzyskania elementu w określonym indeksie. W przeciwnym razie ta metoda uzyskuje określony element.

Wartością domyślną dla typów referencyjnych i dopuszczalnych wartości null jest null.

Dotyczy

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

Zwraca element w określonym indeksie w sekwencji lub wartość domyślną, jeśli indeks jest poza zakresem.

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

Parametry typu

TSource

Typ elementów elementu source.

Parametry

source
IEnumerable<TSource>

Element IEnumerable<T> do zwrócenia elementu z.

index
Int32

Indeks zerowy elementu do pobrania.

Zwraca

TSource

default(TSource) jeśli indeks znajduje się poza granicami sekwencji źródłowej; w przeciwnym razie element znajdujący się na określonej pozycji w sekwencji źródłowej.

Wyjątki

source to null.

Przykłady

W poniższym przykładzie kodu pokazano, jak używać polecenia ElementAtOrDefault. W tym przykładzie użyto indeksu, który znajduje się poza granicami tablicy.

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]

Uwagi

Jeśli typ source implementuje IList<T>, ta implementacja jest używana do uzyskania elementu w określonym indeksie. W przeciwnym razie ta metoda uzyskuje określony element.

Wartością domyślną dla typów referencyjnych i dopuszczalnych wartości null jest null.

Dotyczy