Enumerable.ElementAt Metódus
Definíció
Fontos
Egyes információk olyan, kiadás előtti termékekre vonatkoznak, amelyek a kiadásig még jelentősen módosulhatnak. A Microsoft nem vállal kifejezett vagy törvényi garanciát az itt megjelenő információért.
Túlterhelések
| Name | Description |
|---|---|
| ElementAt<TSource>(IEnumerable<TSource>, Index) |
Egy adott index elemet ad vissza egy sorozatban. |
| ElementAt<TSource>(IEnumerable<TSource>, Int32) |
Egy adott index elemet ad vissza egy sorozatban. |
ElementAt<TSource>(IEnumerable<TSource>, Index)
- Forrás:
- ElementAt.cs
- Forrás:
- ElementAt.cs
- Forrás:
- ElementAt.cs
- Forrás:
- ElementAt.cs
- Forrás:
- ElementAt.cs
Egy adott index elemet ad vissza egy sorozatban.
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
Típusparaméterek
- TSource
A . elemeinek sourcetípusa.
Paraméterek
- source
- IEnumerable<TSource>
Egy IEnumerable<T> elem visszaadásához.
- index
- Index
A lekérendő elem indexe, amely a sorozat elejétől vagy végétől számít.
Válaszok
A sorrendben megadott pozícióban lévő source elem.
Kivételek
source az null.
index kívül esik a source sorozat határain.
Megjegyzések
Ha a implementáció típusa sourceIList<T>, akkor a rendszer ezt a megvalósítást használja az elem lekérésére a megadott indexben. Ellenkező esetben ez a metódus beolvasja a megadott elemet.
Ez a metódus kivételt eredményez, ha index nincs tartományon kívül. Ha ehelyett alapértelmezett értéket szeretne visszaadni, ha a megadott index túllépi a tartományt, használja a metódust ElementAtOrDefault .
A következőre érvényes:
ElementAt<TSource>(IEnumerable<TSource>, Int32)
- Forrás:
- ElementAt.cs
- Forrás:
- ElementAt.cs
- Forrás:
- ElementAt.cs
- Forrás:
- ElementAt.cs
- Forrás:
- ElementAt.cs
Egy adott index elemet ad vissza egy sorozatban.
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
Típusparaméterek
- TSource
A . elemeinek sourcetípusa.
Paraméterek
- source
- IEnumerable<TSource>
Egy IEnumerable<T> elem visszaadásához.
- index
- Int32
A lekérni kívánt elem nulla alapú indexe.
Válaszok
A forrásütemezésben megadott pozícióban lévő elem.
Kivételek
source az null.
index kisebb, mint 0, vagy nagyobb, mint a source.
Példák
Az alábbi példakód bemutatja, hogyan lehet ElementAt egy adott helyzetben visszaadni egy elemet.
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
Megjegyzések
Ha a implementáció típusa sourceIList<T>, akkor a rendszer ezt a megvalósítást használja az elem lekérésére a megadott indexben. Ellenkező esetben ez a metódus beolvasja a megadott elemet.
Ez a metódus kivételt eredményez, ha index nincs tartományon kívül. Ha ehelyett alapértelmezett értéket szeretne visszaadni, ha a megadott index túllépi a tartományt, használja a metódust ElementAtOrDefault .