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
Understanding IndexOutOfRangeException Exception in C#
The IndexOutOfRangeException is a common runtime exception in C# that occurs when you attempt to access an array element using an index that is outside the valid range of indices for that array. This exception helps prevent memory corruption by catching invalid array access attempts.
Arrays in C# are zero-indexed, meaning the first element is at index 0, and the last element is at index length - 1. Attempting to access an index less than 0 or greater than or equal to the array length will throw this exception.
When IndexOutOfRangeException Occurs
This exception is thrown in the following scenarios −
Accessing an array index that is negative
Accessing an array index that is greater than or equal to the array length
Using incorrect loop bounds when iterating through arrays
Accessing collection elements with invalid indices
Example: Array Index Out of Bounds
Here's an example that demonstrates the exception when accessing an invalid array index −
using System;
class Program {
static void Main(string[] args) {
int[] arr = new int[5];
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
Console.WriteLine("Array elements:");
for (int i = 0; i
The output of the above code is −
Array elements:
arr[0] = 10
arr[1] = 20
arr[2] = 30
arr[3] = 40
arr[4] = 50
Exception caught: Index was outside the bounds of the array.
Example: Negative Index Access
Attempting to access an array with a negative index also throws this exception −
using System;
class Program {
static void Main(string[] args) {
string[] colors = {"Red", "Green", "Blue"};
try {
Console.WriteLine(colors[-1]); // Negative index
}
catch (IndexOutOfRangeException ex) {
Console.WriteLine("Error: " + ex.Message);
}
try {
Console.WriteLine(colors[3]); // Index equals length
}
catch (IndexOutOfRangeException ex) {
Console.WriteLine("Error: " + ex.Message);
}
}
}
The output of the above code is −
Error: Index was outside the bounds of the array.
Error: Index was outside the bounds of the array.
Preventing IndexOutOfRangeException
You can prevent this exception by checking array bounds before accessing elements −
using System;
class Program {
static void Main(string[] args) {
int[] numbers = {1, 2, 3, 4, 5};
int index = 7;
// Safe array access
if (index >= 0 && index
The output of the above code is −
Index 7 is out of bounds for array of length 5
All elements:
numbers[0] = 1
numbers[1] = 2
numbers[2] = 3
numbers[3] = 4
numbers[4] = 5
Common Prevention Techniques
| Technique | Description | Example |
|---|---|---|
| Bounds Checking | Check if index is within valid range | if (i >= 0 && i |
| Use Length Property | Always use array.Length in loops | for (int i = 0; i |
| Try-Catch Blocks | Handle the exception gracefully | try { arr[i] = value; } catch {...} |
| Foreach Loops | Eliminate index-based access | foreach (var item in arr) |
Conclusion
The IndexOutOfRangeException occurs when accessing array elements with invalid indices (negative or >= array length). Always validate array bounds before access, use the Length property in loops, and consider try-catch blocks for robust error handling.
