C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Note: This means that for an array of 100 elements, you can access array[0] through array[99].
Important: The top index you can access equals the total length minus one. If you access an index past 99, you get an IndexOutOfRangeException.
Elements: You can load and store values into these elements, which are considered variables and not values.
C# program that accesses array out of bounds
class Program
{
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[100];
array[0] = 1;
array[10] = 2;
array[200] = 3;
}
}
Output
Unhandled Exception: System.IndexOutOfRangeException:
Index was outside the bounds of the array.
at Program.Main() in ...Program.cs:line 8
Note: The first element is always available at index zero. There are ways to simulate non-zero based arrays.
Also: This off-by-one difference is critical to keep in mind in some programs. Errors in this translation can be serious.