C# Linq ElementAt Method

The ElementAt() method in C# LINQ returns the element at a specified index position in a sequence. It is part of the LINQ extension methods and can be used with any IEnumerable<T> collection.

The ElementAt() method throws an ArgumentOutOfRangeException if the index is out of bounds. For safer access, you can use ElementAtOrDefault(), which returns the default value for the type if the index is invalid.

Syntax

Following is the syntax for the ElementAt() method −

public static TSource ElementAt<TSource>(this IEnumerable<TSource> source, int index)

Following is the syntax for the ElementAtOrDefault() method −

public static TSource ElementAtOrDefault<TSource>(this IEnumerable<TSource> source, int index)

Parameters

  • source − The IEnumerable<T> to return an element from.

  • index − The zero-based index of the element to retrieve.

Return Value

The ElementAt() method returns the element at the specified position in the source sequence. The ElementAtOrDefault() method returns the element at the specified position or the default value if the index is out of range.

Using ElementAt() with Arrays

Example

using System;
using System.Linq;

class Program {
   static void Main() {
      string[] names = { "Alice", "Bob", "Charlie", "Diana" };
      
      // Get element at index 1
      string secondName = names.ElementAt(1);
      Console.WriteLine("Element at index 1: " + secondName);
      
      // Get element at index 3
      string fourthName = names.ElementAt(3);
      Console.WriteLine("Element at index 3: " + fourthName);
   }
}

The output of the above code is −

Element at index 1: Bob
Element at index 3: Diana

Using ElementAtOrDefault() for Safe Access

Example

using System;
using System.Linq;

class Program {
   static void Main() {
      int[] numbers = { 10, 20, 30, 40, 50 };
      
      // Valid index
      int element = numbers.ElementAtOrDefault(2);
      Console.WriteLine("Element at index 2: " + element);
      
      // Invalid index - returns default value (0 for int)
      int invalidElement = numbers.ElementAtOrDefault(10);
      Console.WriteLine("Element at index 10: " + invalidElement);
      
      // With strings - returns null for invalid index
      string[] fruits = { "Apple", "Banana", "Orange" };
      string validFruit = fruits.ElementAtOrDefault(1);
      string invalidFruit = fruits.ElementAtOrDefault(5);
      
      Console.WriteLine("Valid fruit: " + validFruit);
      Console.WriteLine("Invalid fruit: " + (invalidFruit ?? "null"));
   }
}

The output of the above code is −

Element at index 2: 30
Element at index 10: 0
Valid fruit: Banana
Invalid fruit: null

Using ElementAt() with LINQ Queries

Example

using System;
using System.Linq;

class Program {
   static void Main() {
      int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
      
      // Get the 3rd even number
      int thirdEven = numbers.Where(x => x % 2 == 0).ElementAt(2);
      Console.WriteLine("Third even number: " + thirdEven);
      
      // Get the 2nd number greater than 5
      int secondGreaterThan5 = numbers.Where(x => x > 5).ElementAt(1);
      Console.WriteLine("Second number greater than 5: " + secondGreaterThan5);
   }
}

The output of the above code is −

Third even number: 6
Second number greater than 5: 7

Comparison

Method Behavior on Invalid Index Use Case
ElementAt() Throws ArgumentOutOfRangeException When you are certain the index is valid
ElementAtOrDefault() Returns default value for the type When index validity is uncertain
Array indexer [index] Throws IndexOutOfRangeException Direct array access (fastest)

Conclusion

The ElementAt() method provides a LINQ-based way to access elements by index in any enumerable collection. Use ElementAtOrDefault() when you need safe access that won't throw exceptions for invalid indices, returning default values instead.

Updated on: 2026-03-17T07:04:35+05:30

367 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements