Jamie Dalley

Jamie Dalley

  • NA
  • 16
  • 5.5k

Why am I getting an IndexOutOfRangeException for this?

Jan 21 2013 8:13 AM
Basically I am doing a C# assignment for part of my uni course. This part of it needs to create an array with 10 items. All the items should be random numbers generated between 0 and 19. The i need to use a for loop to print out what each item is. The problem is i am getting an IndexOutOfRangeException and i have no idea why, so was just wondering if anyone could take a look at it and help me out, tanks in advance, here it is:
            int MaxValue;
            int RandomNumber;
            Random MyRandom;
            MaxValue = 20;
            MyRandom = new Random();
            RandomNumber = MyRandom.Next(MaxValue);

            int[] firstArray = new int[9];
            for (int i = 0; i < 10; i++)
            {
                firstArray[i] = RandomNumber;
            }
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("Item {0} in the array is {1}", i, firstArray[i]);
            }

Answers (2)