Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

C# LINQ ElementAt: Usage, Performance, and Alternatives

Learn how C# LINQ ElementAt accesses elements by index, when it is O(1) or O(n), and when alternatives such as ElementAtOrDefault or direct indexing are better.

~5 min readC#
#LINQ#C##ElementAt#IEnumerable
Illustration of C# LINQ ElementAt selecting an element by index from a sequence.

When you need to retrieve a specific element from a sequence by its position, C# LINQ provides the ElementAt method. This article covers how ElementAt works, its runtime behavior, and the tradeoffs involved in using it.

ElementAt Syntax and Basic Usage

The ElementAt method is part of the System.Linq namespace and is available as an extension method on any IEnumerable<T>. It takes a zero-based index and returns the element at that position. Here is the simplest form:

csharp
using System; using System.Linq; var numbers = new[] { 10, 20, 30, 40, 50 }; int third = numbers.ElementAt(2); Console.WriteLine(third); // Output: 30

The index is zero-based, so ElementAt(0) returns the first element. In LINQ to Objects, if the index is negative or greater than or equal to the sequence length, ElementAt throws an ArgumentOutOfRangeException. Other LINQ providers may translate ElementAt differently, so check the provider's documentation when you are not working with an in-memory IEnumerable<T>.

How ElementAt Works with IEnumerable

ElementAt is an extension method on IEnumerable<T>, but its actual behavior depends on the underlying type. When the source implements IList<T>, LINQ can use the indexer directly, making the operation O(1). For example, arrays and List<T> implement IList<T>, so ElementAt uses the indexer directly instead of enumerating the sequence.

However, when the source is a pure IEnumerable<T> that does not implement IList<T>, ElementAt must enumerate the sequence from the beginning until it reaches the requested index. This makes the operation O(n) in the worst case, where n is the index. Consider a generator:

csharp
IEnumerable<int> GenerateNumbers() { for (int i = 0; i < 1000; i++) { yield return i; } } var seq = GenerateNumbers(); int value = seq.ElementAt(500); // Enumerates 501 elements

In this example, ElementAt does not know the length of the sequence, so it must iterate through 501 items before returning the result. This behavior is important when working with deferred execution and infinite sequences. For an infinite sequence, any finite non-negative index is reachable, so ElementAt will eventually complete when called with such an index. If the source is finite and the index is beyond its end, ElementAt enumerates to the end and then throws.

ElementAt vs ElementAtOrDefault

LINQ also provides ElementAtOrDefault, which returns the default value of the type (e.g., null for reference types, 0 for numeric types) instead of throwing an exception when the index is out of range. This is useful when you are not sure whether the index exists and you want to avoid exception handling.

csharp
var list = new List<string> { "a", "b", "c" }; string result = list.ElementAtOrDefault(5); // returns null

Use ElementAtOrDefault when a missing element is an expected condition that should not interrupt control flow. Use ElementAt when the index must be valid and an exception is the appropriate failure signal. The performance characteristics are identical because both methods use the same underlying access logic.

Performance Considerations for ElementAt

The performance of ElementAt is directly tied to the underlying collection type. For types that implement IList<T>, such as arrays, List<T>, and Collection<T>, the operation is O(1). For other IEnumerable<T> sources, it is O(n). This distinction matters in performance-sensitive code paths.

If you are repeatedly calling ElementAt on the same non-list sequence, each call re-enumerates from the start. For example, a loop that accesses multiple indices will be O(n*m) if each access is O(n). In such cases, materializing the sequence into a list or array first can be more efficient:

csharp
var seq = GetExpensiveSequence(); var list = seq.ToList(); // O(n) int first = list[0]; // O(1) int second = list[1]; // O(1)

This tradeoff is worth considering when the sequence is finite and you need multiple random accesses. However, materializing an infinite sequence is impossible, so you must rely on the enumeration approach or restructure the algorithm.

When to Use ElementAt and When to Avoid It

Use ElementAt when you need a single element at a known index and the source is already a list or array. In that case, it is a clean, readable way to express intent, and the asymptotic performance is the same as direct indexing.

Avoid ElementAt on a non-list IEnumerable when you only need the first or last element. For the first element, use First() or FirstOrDefault(). For the last element, use Last() or LastOrDefault(), which are optimized for some sequences but still enumerate fully for others. If you need to iterate through a sequence and access elements by index, consider using a for loop with a list instead of repeated ElementAt calls.

Another common pattern is using ElementAt to implement random access on a sequence that is not indexable. If you find yourself doing this frequently, it is often better to convert the sequence to a list once and then use standard indexing. This keeps the code simpler and avoids hidden O(n) costs.

Common Mistakes and Edge Cases

A frequent mistake is assuming ElementAt works like an array indexer for any IEnumerable. This leads to unexpected performance degradation when the source is a yield iterator or a database query. Always check whether the type implements IList<T> if performance matters.

Another edge case is passing a negative index. Both ElementAt and ElementAtOrDefault treat negative indices as invalid. ElementAt throws, while ElementAtOrDefault returns the default value. There is no special handling for negative indices; they are always out of range.

Empty sequences also behave predictably: ElementAt throws for any index, and ElementAtOrDefault returns the default value. This is consistent with the documentation and should be handled in your code if empty sequences are possible.

Alternative Approaches to Element Access

Depending on your goal, there are alternatives to ElementAt. If you need the first or last element, First, Last, and their OrDefault variants are more idiomatic. If you need a range of elements, Skip and Take can be combined to extract a slice without materializing the entire sequence:

csharp
var slice = seq.Skip(10).Take(5); // elements at indices 10-14

For direct indexing on a known list type, the indexer list[index] is more explicit and avoids the extra method call. The only advantage of ElementAt is that it works uniformly on any IEnumerable, which can be useful in generic code that accepts IEnumerable<T>.

When writing generic methods that receive IEnumerable<T>, you can improve performance by checking for IList<T> and using the indexer when available:

csharp
public static T GetAt<T>(IEnumerable<T> source, int index) { if (source is IList<T> list) { return list[index]; } return source.ElementAt(index); }

This pattern preserves the generic contract while avoiding the O(n) cost for common list types. It is a practical optimization that keeps the code maintainable and correct.

Try matching browser tools

All tools

Small utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.

All C# guides

More in C#

All C# guides

The guides filed next to this one in C#, in library order.