Index out of range exception

When reference an index behind the length of an array C# generates and throws IndexOfOfRange exception.

The following code generates the IndexOutOfRange exception.


using System;

class Program
{
    static void Main(string[] args)
    {
        int[] intArray = new int[3];
        Console.WriteLine(intArray[10]);

    }
}

The output:


Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at Program.Main(String[] args)
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.