Exception example. When you create an array of many elements, you can access those elements through the indices of zero through the maximum length minus one.
Note This means that for an array of 100 elements, you can access array through array.
Important The top index equals the total length minus one. If you access an index past 99, you get an IndexOutOfRangeException.
Detail You can load and store values into these elements, which are considered variables and not values.
static void Main()
// Allocate an array of one-hundred integers.
// ... Then assign to positions in the array.
// ... Assigning past the last element will throw.
int array = new int;
array = 1;
array = 2;
array = 3;
}Unhandled Exception: System.IndexOutOfRangeException:
Index was outside the bounds of the array.
at Program.Main() in ...Program.cs:line 8
To avoid the exception, you can insert bounds checks, as with an if-statement. An alternative is to simply make the array length much larger when you allocate it.