title: Index out of Range exception in C# when iterating through an array
body: <p>I have this code I'm using to create a program that takes a a range and outputs to the Console the prime numbers. I have one problem, I'm trying to iterate through the array I built so the loop should only write to the console the values that are prime using my method's return value. The problem I'm having is that I have the second condition set to <code>numArray.Length</code> but it seems to give me the <code>Index out of Range Exception</code>. I just want the loop to iterate through all values in the <code>numArray</code> and stop when it's done figuring out whether the last value is prime or not.</p>
  
  <pre><code>public struct Prime
  {
      public int x;
  
  
      // constructor for Prime
      public Prime(int x1)
      {
          x = x1;
  
      }
  
      public int IsPrime(int number)
      {
          int i;
          for (i = 2; i * i &lt;= number; i++)
          {
              if (number % i == 0) return 0;
          }
          return 1;
      }
  }
  
  class Program
  {
      static void Main(string[] args)
      {
          Console.WriteLine("Enter an Integer");
          int num1 = Convert.ToInt32(Console.ReadLine());
          Console.WriteLine("Enter a Second Integer of Greater Value");
      //    int num2 = 0;
          int num2 = Convert.ToInt32(Console.ReadLine());
  
      /*    if (num2temp &gt; num1)
          {
              num2temp = num2;
          }
          else
          {
              Console.WriteLine("You Did Not Enter An Integer Greater Than the First Integer, Please Enter Your Integers Again.");
              Environment.Exit(0);
          }
      */    int index = 1;
          int[] numArray = new int[num2];
          for (int i = num1; i &lt;= num2; i++)
          {
              numArray[index] = i;
  
              Console.WriteLine(" index: {0} assignment: {1}", index, i);
              index++;
              Console.WriteLine("index: {0}",index);
          }
  
          Console.WriteLine("value: {0}", numArray[40]);
  
      /*    Prime myprime = new Prime();
  
          if (myprime.IsPrime(numArray[12]) == 1)
          {
              Console.WriteLine("true");
          }
          else
          {
              Console.WriteLine("False");
          } */
  
  
          Prime myprime = new Prime();
  
  
         int value = 0;
          for (int y = 1; y &lt;= num2; y++)
          {
              if (myprime.IsPrime(numArray[y]) == 1)
              {
                  value = numArray[y];
                  Console.Write("{0} \t", value);
  
              }
  
          } 
  </code></pre>