title: C# for and foreach loop difference
body: <p>I am using C# 8.0. Given the following code block,</p>
  
  <pre><code>    int[] data;
      string str1 = "";
      int size;
      Console.WriteLine("\n Input array size:");
      size = int.Parse(Console.ReadLine());
      if (size &gt;= 1 &amp;&amp; size &lt;=9)
      {
        data = new int[size];
        for (int i = 0; i &lt; data.Length; i++)
        {
          data[i] = 111 * (i+1);
          foreach (int num in data)
          {
            str1 += "\n data["+i.ToString()+"] =" + num.ToString() + "\n";
  
          }
          i++;
        }
        Console.WriteLine("{0}", str1);
      }
      else
        Console.WriteLine("Out of range!");
  </code></pre>
  
  <p>I get the following result:</p>
  
  <pre><code>Input array size:
  3
  
   data[0] =111
  
   data[0] =0
  
   data[0] =0
  
   data[2] =111
  
   data[2] =0
  
   data[2] =333
  
  </code></pre>
  
  <p>Ideally, I would like output similar to the following:</p>
  
  <pre><code>data[0] =111
  data[1] =222
  data[2] =333
  
  </code></pre>
  
  <p>May I know how to revise the code, and why?
  I've only started learning C# recently.</p>
  
  <p>Edit:</p>
  
  <pre><code>if (size &gt;= 1 &amp;&amp; size &lt;=9)
      {
        data = new int[size];
        for (int i = 0; i &lt; data.Length; i++)
          data[i] = 111 * (i+1);
        foreach (int num in data)
          {
            str1 += "\n data["+i.ToString()+"] = " + num.ToString() + "\n";
            i++;
          }
        Console.WriteLine("{0}", str1);
      }
  </code></pre>
  
  <p>This was the original version from a book, but I was trying to fix the compiling error.</p>
  
  <pre><code>main.cs(18,30): error CS0103: The name `i' does not exist i
  n the current context
  main.cs(19,11): error CS0103: The name `i' does not exist i
  n the current context
  Compilation failed: 2 error(s), 0 warnings
  compiler exit status 1
  </code></pre>