What is the output of the following code?
using System; class MainClass { public static void Main(string[] args) { int[] arr = new int[3]; arr[3] = 1; // IndexOutOfRangeException thrown Console.WriteLine(arr[3]); } }
arr[3] = 1; // IndexOutOfRangeException thrown
All array indexing is bounds-checked by the runtime.
An IndexOutOfRangeException is thrown if you use an invalid index:
In the following code
int[] arr = new int[3];
defines an array with 3 element and the array index is started at 0. Therefore the array element are arr[0], arr[1], arr[2].