Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
ElementAt() method in C#
The ElementAt() method in C# is a LINQ extension method that retrieves an element at a specified index from any collection that implements IEnumerable<T>. It provides a way to access elements by index in collections that don't have built-in indexing support.
Syntax
Following is the syntax for the ElementAt() method −
public static TSource ElementAt<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
Returns the element at the specified position in the source sequence. Throws ArgumentOutOfRangeException if the index is less than 0 or greater than or equal to the number of elements in the source.
Using ElementAt() with Arrays
Example
using System;
using System.Linq;
public class Demo {
public static void Main() {
string[] arr = { "One", "Two", "Three", "Four", "Five" };
// Get element at index 0
string firstElement = arr.ElementAt(0);
Console.WriteLine("Element at index 0: " + firstElement);
// Get element at index 3
string fourthElement = arr.ElementAt(3);
Console.WriteLine("Element at index 3: " + fourthElement);
}
}
The output of the above code is −
Element at index 0: One Element at index 3: Four
Using ElementAt() with Lists
Example
using System;
using System.Collections.Generic;
using System.Linq;
public class Demo {
public static void Main() {
List<int> numbers = new List<int> { 10, 20, 30, 40, 50 };
// Get element at index 2
int thirdElement = numbers.ElementAt(2);
Console.WriteLine("Element at index 2: " + thirdElement);
// Get last element using Count - 1
int lastElement = numbers.ElementAt(numbers.Count - 1);
Console.WriteLine("Last element: " + lastElement);
}
}
The output of the above code is −
Element at index 2: 30 Last element: 50
ElementAt() vs ElementAtOrDefault()
The ElementAtOrDefault() method returns the default value for the type if the index is out of range, instead of throwing an exception −
Example
using System;
using System.Linq;
public class Demo {
public static void Main() {
string[] arr = { "Apple", "Banana", "Cherry" };
// Valid index
Console.WriteLine("ElementAt(1): " + arr.ElementAt(1));
Console.WriteLine("ElementAtOrDefault(1): " + arr.ElementAtOrDefault(1));
// Invalid index - ElementAtOrDefault returns null for reference types
Console.WriteLine("ElementAtOrDefault(10): " + (arr.ElementAtOrDefault(10) ?? "null"));
// ElementAt(10) would throw ArgumentOutOfRangeException
}
}
The output of the above code is −
ElementAt(1): Banana ElementAtOrDefault(1): Banana ElementAtOrDefault(10): null
Common Use Cases
LINQ Queries − Accessing elements by index in the result of a LINQ query.
Collections without indexers − Accessing elements by index in collections like
HashSet<T>.Method chaining − Using within LINQ method chains for more complex operations.
Conclusion
The ElementAt() method provides indexed access to any IEnumerable<T> collection. Use ElementAt() when you need to throw an exception for invalid indices, or ElementAtOrDefault() when you prefer to handle invalid indices gracefully with default values.
