Enumerable.ElementAtOrDefault Metoda

Definice

Přetížení

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

Vrátí prvek v zadaném indexu v sekvenci nebo výchozí hodnotu, pokud je index mimo rozsah.

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

Vrátí prvek v zadaném indexu v sekvenci nebo výchozí hodnotu, pokud je index mimo rozsah.

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

Zdroj:
ElementAt.cs
Zdroj:
ElementAt.cs
Zdroj:
ElementAt.cs
Zdroj:
ElementAt.cs
Zdroj:
ElementAt.cs

Vrátí prvek v zadaném indexu v sekvenci nebo výchozí hodnotu, pokud je index mimo rozsah.

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 prvků .source

Parametry

source
IEnumerable<TSource>

Vrácení IEnumerable<T> elementu z.

index
Index

Index prvku, který se má načíst, je buď od začátku, nebo na konci sekvence.

Návraty

TSource

default je-li index mimo hranice source sekvence; v opačném případě prvek na zadané pozici v posloupnosti source .

Výjimky

source je null.

Poznámky

Pokud typ source implements IList<T>, tato implementace se používá k získání elementu v zadaném indexu. Jinak tato metoda získá zadaný prvek.

Výchozí hodnota pro odkazy a typy s možnou hodnotou null je null.

Platí pro

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

Zdroj:
ElementAt.cs
Zdroj:
ElementAt.cs
Zdroj:
ElementAt.cs
Zdroj:
ElementAt.cs
Zdroj:
ElementAt.cs

Vrátí prvek v zadaném indexu v sekvenci nebo výchozí hodnotu, pokud je index mimo rozsah.

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 prvků .source

Parametry

source
IEnumerable<TSource>

Vrácení IEnumerable<T> elementu z.

index
Int32

Index elementu založený na nule, který se má načíst.

Návraty

TSource

default(TSource) pokud je index mimo hranice zdrojové sekvence; jinak prvek na zadané pozici ve zdrojové sekvenci.

Výjimky

source je null.

Příklady

Následující příklad kódu ukazuje, jak použít ElementAtOrDefault. Tento příklad používá index, který je mimo hranice pole.

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]

Poznámky

Pokud typ source implements IList<T>, tato implementace se používá k získání elementu v zadaném indexu. Jinak tato metoda získá zadaný prvek.

Výchozí hodnota pro odkazy a typy s možnou hodnotou null je null.

Platí pro