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.

C#
public static TSource ElementAt<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Index index);

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

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

.NET 9 i inne wersje
Produkt Wersje
.NET 6, 7, 8, 9

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.

C#
public static TSource ElementAt<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, int index);

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

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

Wyjątki

source to null.

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

Przykłady

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

C#
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'.
*/

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

.NET 9 i inne wersje
Produkt Wersje
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0