IndexOutOfRangeException
C# Programming Language
Severity: ModerateWhat Does This Error Mean?
IndexOutOfRangeException means you tried to access a slot in an array that does not exist. For example: an array has 3 items (slots 0, 1, 2), and you tried to read slot 5. C# throws this exception immediately because that memory location is invalid. It always means your index number is too large, too small, or calculated incorrectly.
Affected Models
- .NET Framework
- .NET Core
- .NET 5+
- Unity (C# scripting)
- ASP.NET
Common Causes
- Using a hardcoded index that is larger than the array's actual length
- A loop counter goes one step too far — classic 'off by one' mistake
- The array was declared with a size, but fewer items were added than expected
- An index is calculated from user input or data that turns out to be larger than the array
- Trying to access index 0 on an empty array (which has no valid slots at all)
How to Fix It
-
Check the error message for the file name and line number. Go to that line and find the array access — something like myArray[index].
The problem is always on the line shown. Look for square brackets — those are the array accesses.
-
Print or log the value of your index variable and the length of the array just before the crash. Compare them.
Example: Console.WriteLine('Index: ' + index + ', Length: ' + myArray.Length); — this quickly shows if the index is out of range.
-
Make sure your loop ends at the correct position. Array indices start at 0, so the last valid index is always Length - 1.
Correct loop: for (int i = 0; i < myArray.Length; i++) — note the strict less-than, not less-than-or-equal.
-
Add a bounds check before accessing the array with a calculated or unknown index.
Example: if (index >= 0 && index < myArray.Length) { use myArray[index]; } — this prevents the crash entirely.
-
Consider switching to a List<T> instead of a plain array. Lists have helpful methods like Count and will not let you access invalid positions as easily.
Lists are more flexible than arrays and easier to work with when the size can change.
When to Call a Professional
IndexOutOfRangeException is always a code bug that you can fix yourself. If the array content comes from a database or external source, make sure to validate the data before processing. For complex algorithms involving multi-dimensional arrays, a code review from a colleague can help catch logic errors.
Frequently Asked Questions
What is the difference between IndexOutOfRangeException and ArgumentOutOfRangeException?
They are very similar but happen in different places. IndexOutOfRangeException always comes from accessing an array with a bad index. ArgumentOutOfRangeException comes from passing a bad value to a method — like List.RemoveAt(-1) or String.Substring(100) on a short string. Both mean the same thing: a number is outside the valid range.
Why does C# start array indexes at 0 instead of 1?
This is a convention inherited from C and C++, and nearly all modern languages do the same. It relates to how memory addresses are calculated internally. For a 5-item array, the valid indexes are 0, 1, 2, 3, 4. Remembering 'last valid index = Length minus 1' will save you from many of these errors.
Can I check the array length before accessing it?
Yes — and you should, whenever the index is not fixed. Use myArray.Length for arrays, or myList.Count for lists. Always validate before accessing with a dynamic index. This turns a crashing program into one that handles edge cases gracefully.