O'Reilly logo

C# Primer Plus by Klaus Michelsen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required