---
title: "Explain the ElementAt(), and ElementAtOrDefault() functions in LINQ with C#."  
description: "Explain the ElementAt(), and ElementAtOrDefault() functions in LINQ with C#."  
author: "Amrith Chandran"  
published: 2025-02-12  
updated: 2025-02-12  
canonical: https://www.mindstick.com/forum/161156/explain-the-elementat-and-elementatordefault-functions-in-linq-with-c-sharp  
category: "linq"  
tags: ["linq", "linq function"]  
reading_time: 2 minutes  

---

# Explain the ElementAt(), and ElementAtOrDefault() functions in LINQ with C#.

[Explain](https://www.mindstick.com/forum/157854/what-is-system-debugging-explain-some-system-debugging-tools-used-in-modern-computer-systems) the ElementAt(), and ElementAtOrDefault() functions in LINQ with C#.

## Replies

### Reply by Ashutosh Patel

#### ElementAt() vs ElementAtOrDefault() in LINQ (C#)

**ElementAt()** and **ElementAtOrDefault()** are both used to retrieve the element at a specific index in a collection, but they handle out-of-range indexes differently.

#### ElementAt()

- Returns the element at the specified index.
- Throws an exception (ArgumentOutOfRangeException) if the index is out of range.

## Example (Retrieving an Element by Index)

```cs
using System;
using System.Linq;
using System.Collections.Generic;
class Program
{
   static void Main()
   {
       List<int> numbers = new List<int> { 10, 20, 30, 40, 50 };
       int element = numbers.ElementAt(2); // Gets the 3rd element (index starts at 0)
       Console.WriteLine("Element at index 2: " + element);
       // Output: Element at index 2: 30

       // Below code return out of range an error
       List<int> numbers2 = new List<int> { 10, 20, 30 };
       int element2 = numbers2.ElementAt(5); // Index 5 is out of range
       // Throws: ArgumentOutOfRangeException: "Index was out of range"
   }
}
```

#### ElementAtOrDefault()

- Returns the element at the specified index.
- If the index is out of range, it returns the default value (`null` for reference types, `0` for numeric types,`false` for bool).

```cs
using System;
using System.Linq;
using System.Collections.Generic;

class Program
{
   static void Main()
   {
       List<int> numbers = new List<int> { 10, 20, 30, 40, 50 };
       int element = numbers.ElementAtOrDefault(2);
       Console.WriteLine("Element at index 2: " + element);
       // Output: Element at index 2: 30
       int outOfRangeElement = numbers.ElementAtOrDefault(5);
       Console.WriteLine("Element at index 5: " + outOfRangeElement);
       // Output: Element at index 5: 0 (default for int)

       // for string type
       List<string> names = new List<string> { "Alice", "Bob", "Charlie" };
       string result = names.ElementAtOrDefault(4);
       Console.WriteLine(result == null ? "No element found" : result);
       // Output: No element found (since index 4 is out of range)
   }
}
```

## When to use what?

- When you are sure the index exists, use `ElementAt()`.
- When the index is out of range, use `ElementAtOrDefault()` to prevent exceptions.

**Also, Read:** [What are LINQ Last(), and LastOrDefault() functions?](https://www.mindstick.com/forum/161155/what-are-linq-last-and-lastordefault-functions)


---

Original Source: https://www.mindstick.com/forum/161156/explain-the-elementat-and-elementatordefault-functions-in-linq-with-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
