Author Topic: C# Arrays - "Index was outside the bounds of the array."  (Read 971 times)

administrator

  • Administrator
  • Full Member
  • *****
  • Posts: 69
C# Arrays - "Index was outside the bounds of the array."
« on: November 01, 2011, 12:09:41 pm »
The most common error with using Arrays in C# is: "Index was outside the bounds of the array."

For example,  you have allocated 3 integers and the valid indices are 0 - 2. If you go over 2, that is 3 or 4, etc, then you would get this error. The following lines of code would simulate this error:


                int[] arguments = new int[3] { 5, 10, 15};

                foreach (int m in arguments)
                   Console.WriteLine(m);

                for (int i = 0; i <= arguments.Length; i++)
                   Console.WriteLine(arguments);



The output would be,

5
10
15
5
10
15
Index was outside the bounds of the array.

Press any key to continue . . .

 


Disclaimer: This web site is for educational and informational purposes only. Click here to read the Disclaimer.
Content copyright 2010-2014. Kathiresan. All rights reserved.
This page is using SMF version 2.0.1