Enumerable.ElementAt Metoda

Definicja

Przeciążenia

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

Zwraca element w określonym indeksie w sekwencji.

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

Zwraca element w określonym indeksie w sekwencji.

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

Źródło:
ElementAt.cs
Źródło:
ElementAt.cs
Źródło:
ElementAt.cs

Zwraca element w określonym indeksie w sekwencji.

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

Parametry typu

TSource

Typ elementów elementu source.

Parametry

source
IEnumerable<TSource>

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

index
Index

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

Zwraca

TSource

Element w określonej pozycji w source sekwencji.

Wyjątki

source to null.

index znajduje się poza granicami source sekwencji.

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.

Ta metoda zgłasza wyjątek, jeśli index jest poza zakresem. Aby zamiast tego zwrócić wartość domyślną, gdy określony indeks jest poza zakresem, użyj ElementAtOrDefault metody .

Dotyczy

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

Źródło:
ElementAt.cs
Źródło:
ElementAt.cs
Źródło:
ElementAt.cs

Zwraca element w określonym indeksie w sekwencji.

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

Parametry typu

TSource

Typ elementów elementu source.

Parametry

source
IEnumerable<TSource>

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

index
Int32

Indeks zerowy elementu do pobrania.

Zwraca

TSource

Element w określonej pozycji w sekwencji źródłowej.

Wyjątki

source to null.

index wartość jest mniejsza niż 0 lub większa niż lub równa liczbie elementów w elemecie source.

Przykłady

W poniższym przykładzie kodu pokazano, jak użyć ElementAt polecenia , aby zwrócić element w określonej pozycji.

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

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.

Ta metoda zgłasza wyjątek, jeśli index jest poza zakresem. Aby zamiast tego zwrócić wartość domyślną, gdy określony indeks jest poza zakresem, użyj ElementAtOrDefault metody .

Dotyczy