title: Error message "Index was outside the bounds of the array"
body: <p>I have this code and when I run it the error is in   <code>new Program(int.Parse(args[0]));</code> and the exception is </p>
  
  <blockquote>
    <p>Index was outside the bounds of the array </p>
  </blockquote>
  
  <p>My code: </p>
  
  <pre><code>  using System;
  using System.Threading.Tasks;
  using System.Linq;
  
  namespace MagicSquares
  {
      class Program
      {
          static void Main(string[] args)
          {
              if (args.Length &gt; 0)
          new Program(int.Parse(args[0]));
  
          }
  
          private int n;
          private int constant;
          private int[,] solution;
  
          public Program(int n)
          {
              this.n = n;
              int nSquared = n * n;
              int sumAllNumbs = nSquared * (nSquared + 1) / 2;
              constant = sumAllNumbs / n;
  
              int threads = Environment.ProcessorCount;
              Parallel.For(0, threads, (i) =&gt;
              {
                  Random rnd = new Random();
                  int[] numbers = Enumerable.Range(1, nSquared).ToArray();
                  int[,] square = new int[n, n];
  
                  do
                  {
                      RandomizeNumbers(rnd, numbers);
                      FillSquare(square, numbers);
  
                      if (IsMagicSquare(square))
                          solution = square;
  
                  } while (solution == null);
              });
  
              PrintSquare(solution);
          }
  
          private void RandomizeNumbers(Random rnd, int[] numbers)
          {
              for (int i = numbers.Length - 1; i &gt;= 0; i--)
              {
                  int index = rnd.Next(numbers.Length);
                  int temp = numbers[i];
                  numbers[i] = numbers[index];
                  numbers[index] = temp;
              }
          }
  
          private void FillSquare(int[,] square, int[] numbers)
          {
              int index = 0;
              for (int y = 0; y &lt; n; y++)
                  for (int x = 0; x &lt; n; x++)
                  {
                      square[y, x] = numbers[index];
                      index++;
                  }
          }
  
          private bool IsMagicSquare(int[,] square)
          {
              //Check horizontal
              for (int y = 0; y &lt; n; y++)
              {
                  int sum = 0;
                  for (int x = 0; x &lt; n; x++)
                      sum += square[y, x];
  
                  if (sum != constant)
                      return false;
              }
  
              //Check vertical
              for (int x = 0; x &lt; n; x++)
              {
                  int sum = 0;
                  for (int y = 0; y &lt; n; y++)
                      sum += square[y, x];
  
                  if (sum != constant)
                      return false;
              }
  
              return true;
          }
  
          private void PrintSquare(int[,] square)
          {
              for (int y = 0; y &lt; n; y++)
              {
                  for (int x = 0; x &lt; n; x++)
                      Console.Write(square[y, x] + " ");
                  Console.WriteLine();
              }
          }
      }
  }
  </code></pre>