Mono Class Library: System Namespace

System.IndexOutOfRangeException Class

Represents the error that occurs when an attempt is made to access an element of an array with an index that is outside the bounds of the array. [Edit]

See Also: IndexOutOfRangeException Members

System.Object
     System.Exception
          System.SystemException
               System.IndexOutOfRangeException

[System.Runtime.InteropServices.ComVisible(true)]
public sealed class IndexOutOfRangeException : SystemException

Thread Safety

All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe.

Remarks

Note:

The following CIL instructions throw IndexOutOfRangeException :

  • ldelem.<type>
  • ldelema
  • stelem.<type>
[Edit]

Example

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()
 

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0