---
title: "What is IndexOutOfRangeException in C#"  
description: "What is IndexOutOfRangeException in C#"  
author: "Ravi Vishwakarma"  
published: 2024-06-07  
updated: 2024-06-07  
canonical: https://www.mindstick.com/interview/33897/what-is-indexoutofrangeexception-in-c-sharp  
category: "c#"  
tags: ["c#", ".net"]  
reading_time: 2 minutes  

---

# What is IndexOutOfRangeException in C#

The [**IndexOutOfRangeException**](https://www.mindstick.com/articles/61/exception-handling-in-c-sharp)is an exception in C#, Which arises when you access an **index that is not present** in an **array**, [**collection**](https://www.mindstick.com/articles/11969/collection-in-c-sharp), or other **indexed data structure** that is outside the valid range of indices. This typically happens when you try to access an element at an index that is **less than zero or greater than or equal to the length** of the collection.

Here's an example of how this exception might occur:

```cs
int[] numbers = { 1, 2, 3, 4, 5 };
Console.WriteLine(numbers[5]); // Attempting to access index 5, which is out of bounds
```

When you run this code, it will throw an `IndexOutOfRangeException` because the valid indices for the `numbers` array range from 0 to 4 (inclusive), but we attempted to access **index 5**, which doesn't exist.

To prevent this exception, always ensure that you are accessing indices within the valid range of the collection. You can use **conditional statements** or **loops** to check the bounds before accessing elements or use methods like `TryGetValue` for **dictionaries** or `ElementAtOrDefault` for [**LINQ queries**](https://www.mindstick.com/articles/12007/language-integrated-query-linq-queries), which handle out-of-range situations gracefully by returning default values instead of throwing exceptions.

## Answers

### Answer by Ravi Vishwakarma

The [**IndexOutOfRangeException**](https://www.mindstick.com/articles/61/exception-handling-in-c-sharp)is an exception in C#, Which arises when you access an **index that is not present** in an **array**, [**collection**](https://www.mindstick.com/articles/11969/collection-in-c-sharp), or other **indexed data structure** that is outside the valid range of indices. This typically happens when you try to access an element at an index that is **less than zero or greater than or equal to the length** of the collection.

Here's an example of how this exception might occur:

```cs
int[] numbers = { 1, 2, 3, 4, 5 };
Console.WriteLine(numbers[5]); // Attempting to access index 5, which is out of bounds
```

When you run this code, it will throw an `IndexOutOfRangeException` because the valid indices for the `numbers` array range from 0 to 4 (inclusive), but we attempted to access **index 5**, which doesn't exist.

To prevent this exception, always ensure that you are accessing indices within the valid range of the collection. You can use **conditional statements** or **loops** to check the bounds before accessing elements or use methods like `TryGetValue` for **dictionaries** or `ElementAtOrDefault` for [**LINQ queries**](https://www.mindstick.com/articles/12007/language-integrated-query-linq-queries), which handle out-of-range situations gracefully by returning default values instead of throwing exceptions.


---

Original Source: https://www.mindstick.com/interview/33897/what-is-indexoutofrangeexception-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
