Today, we're going to fix a C# Error: Index was outside the bounds of the array that usually occurs when we try to access the index less than 0 or greater than or equal to the array length - 1. In this study blog, we'll explore why it happens and how to resolve it.
What is "Index was outside the bounds of the array" #
This error happens when we try to access the elements in the array using invalid index. Let's see an example:
int[] numbers = new int[] { 1,2,3,4,5,6,7,8,9 };
for(int i = 0; i <= numbers.Length; i++)
{
Console.WriteLine(numbers[i]);
}
</span>
This is the output:
1
2
3
4
5
6
7
8
9
System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'
Why? Because the numbers array has 9 elements, and thus its length is 9. However, in programming, each collection like array has indices in the range of [0 - N-1], where N = array.length and we can access each element in the array within that range. In the above example, after accessing index 8 with element 9 in numbers array, we tend to access element at index 9, which is an invalid index for numbers array. That's why this error occur.
Let's look at another example:
int[] numbers = new int[] { 1,2,3,4,5,6,7,8,9 };
numbers[-1] = 10;
This is the output:
System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'
Why? we're trying to assign value to the element of numbers array at index -1, which is also an invalid index for numbers array. Hence, the error will also occur.
Causes #
The error, Index was outside the bounds of the array is actually under IndexOutOfRangeException Class in C#. IndexOutOfRangeException: The exception is thrown when the attempt to access the element of a collection or an array using the index outside its bounds is made. Hence, System.IndexOutOfRangeException: 'Index was outside the bounds of the array.' occurs when we're trying to access the element of an array using invalid index. Don't fret; it can be simply resolved.
Every collection such as list, array or the other has only indices in the range of [0, N-1], where N is the length of the array itself.
Solution #
The solution is pretty simple, we just need to ensure we're accessing the elements of the array using the indices within its bounds. Let's say:
int[] numbers = new int[] { 1,2,3,4,5,6,7,8,9 };
for(int i = 0; i < numbers.Length; i++)
{
Console.WriteLine(numbers[i]);
}
This is the output:
1
2
3
4
5
6
7
8
9
or you may also do like this:
int[] numbers = new int[] { 1,2,3,4,5,6,7,8,9 };
for(int i = 0; i <= numbers.Length-1; i++)
{
Console.WriteLine(numbers[i]);
}
</span>
This is the output:
1
2
3
4
5
6
7
8
9
Both examples above are having the same output. What we do is to ensure that the iteration will only be in the range of [0 - N-1], where N = array.length. The numbers array has the length of 9, and thus can only iterate from 0 to 8. These are the valid indices for the numbers array.
You may also use GetLowerBound(Int32) and GetUpperBound(Int32) to avoid this error.
int[] numbers = new int[] { 1,2,3,4,5,6,7,8,9 };
for(int i = numbers.GetLowerBound(0); i <= numbers.GetUpperBound(0); i++)
{
Console.WriteLine(numbers[i]);
}
</span>
This is the output:
1
2
3
4
5
6
7
8
9
For the case above, GetLowerBound(0) is used to get the index of the first element, which is 0 while GetUpperBound(0) is used to get the index of last element. Hence, we can omit the error by using these two functions together.
The dimension for both GetLowerBound(int dimension) and GetUpperBound(int dimension) is determined by the array type, whether it is one-dimensional array or two-dimensional array as they have the different Rank. Make sure the dimension has the value of greater than 0 or Rank - 1.
The Conclusion #
In this study blog, we've explore what is Index was outside the bounds of the array in C#, why it happens and how to resolve it. You should always be aware with the indices of the array when you want to access its element. Make sure not to access index outside of its bound, which is the range [0 - N - 1], where N = length of the array. Thanks!