Array Indexes Out of Range

The minimum valid index value when referencing an array element is 0, as in accountBalances[0] of Listing 10.2. The maximum value is the array length minus 1, as in accountBalances[4]. A common mistake is to write an expression representing an index value, which is evaluated to be either smaller than zero or larger than the maximum valid index, causing the index to be out of range.

When an index, provided to access an array element, is either smaller than the valid minimum index value, or larger than the maximum valid index, this index is said to be out of range.

An out of range index will trigger the .NET runtime to throw an exception (see the following Note) during program execution, not surprisingly called an IndexOutOfRangeException ...

Get C# Primer Plus now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.