title: Variable scope confusion in C#
body: <p>I have two code samples. The first does not compile, but the second does.</p>
  
  <p><strong>Code Sample 1</strong> <em>(does not compile)</em></p>
  
  <pre><code>public void MyMethod(){
      int i=10;
  
      for(int x=10; x&lt;10; x++) {
          int i=10; // Point1: compiler reports error
          var objX = new MyOtherClass();
      }
  
      var objX = new OtherClassOfMine(); // Point2: compiler reports error
  }
  </code></pre>
  
  <p>I understand why the compiler reports an error at <code>Point1</code>. But I don't understand why it reports an error at <code>Point2</code>. And if you say it is because of the organization inside MSIL, then why does the second code example compile?</p>
  
  <p><strong>Code sample 2</strong> <em>(compiles)</em></p>
  
  <pre><code>public void MyMethod(){
  
      for(int x=10; x&lt;10; x++) {
          int i=10; 
          var objX = new MyOtherClass();
      }
  
      for(int x=10; x&lt;10; x++) {
          int i=10; 
          var objX = new MyOtherClass();
      }
  }
  </code></pre>
  
  <p>If the simple rules of variable scope apply in Code Sample 2, then why don't those same rules apply to Code Sample 1?</p>