Enumerable.ElementAt Method

Definition

Overloads

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

Returns the element at a specified index in a sequence.

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

Returns the element at a specified index in a sequence.

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

Source:
ElementAt.cs
Source:
ElementAt.cs
Source:
ElementAt.cs

Returns the element at a specified index in a sequence.

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

Type Parameters

TSource

The type of the elements of source.

Parameters

source
IEnumerable<TSource>

An IEnumerable<T> to return an element from.

index
Index

The index of the element to retrieve, which is either from the beginning or the end of the sequence.

Returns

TSource

The element at the specified position in the source sequence.

Exceptions

source is null.

index is outside the bounds of the source sequence.

Remarks

If the type of source implements IList<T>, that implementation is used to obtain the element at the specified index. Otherwise, this method obtains the specified element.

This method throws an exception if index is out of range. To instead return a default value when the specified index is out of range, use the ElementAtOrDefault method.

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9

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

Source:
ElementAt.cs
Source:
ElementAt.cs
Source:
ElementAt.cs

Returns the element at a specified index in a sequence.

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

Type Parameters

TSource

The type of the elements of source.

Parameters

source
IEnumerable<TSource>

An IEnumerable<T> to return an element from.

index
Int32

The zero-based index of the element to retrieve.

Returns

TSource

The element at the specified position in the source sequence.

Exceptions

source is null.

index is less than 0 or greater than or equal to the number of elements in source.

Examples

The following code example demonstrates how to use ElementAt to return an element at a specific position.

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

Remarks

If the type of source implements IList<T>, that implementation is used to obtain the element at the specified index. Otherwise, this method obtains the specified element.

This method throws an exception if index is out of range. To instead return a default value when the specified index is out of range, use the ElementAtOrDefault method.

Applies to

.NET 9 and other versions
Product Versions
.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