See Also: IndexOutOfRangeException Members
The following CIL instructions throw IndexOutOfRangeException :
The following example demonstrates an error that causes a IndexOutOfRangeException exception.
C# Example
using System;
public class IndexRangeTest {
public static void Main() {
int[] array = {0,0,0};
try {
for (int i = 0; i<4; i++) {
Console.WriteLine("array[{0}] = {1}",i,array[i]);
}
}
catch (IndexOutOfRangeException e) {
Console.WriteLine("Error: {0}",e);
}
}
}
The output is
Example
array[0] = 0 array[1] = 0 array[2] = 0 Error: System.IndexOutOfRangeException: Index was outside the bounds of the array. at IndexRangeTest.Main()