0

This is just a technical question. When I use a for statement, its iteration variable can't be use outside scope for. But then why the compiler not allow me create a variable with the same name of the for itertation varaible.

for (var i = 0; i < 10; i++){}
var a = i;  // it not allowed this
var i = 3;  // bit it also allowed this, and i think that it is possible for the compiler
Alexander Leyva Caro
  • 1,183
  • 1
  • 12
  • 21
  • 1
    Why *wouldn't* the compiler allow you to create a variable if there isn't one by that name in that scope? – David Nov 07 '14 at 15:00
  • 2
    Answer to first question should be obvious - answer to second question can be found [here](http://stackoverflow.com/questions/2059210/using-a-variable-name-used-in-a-child-scope) – D Stanley Nov 07 '14 at 15:02
  • @David - it doesn't, which is the real question - the comment is not worded properly – D Stanley Nov 07 '14 at 15:02
  • 2
    http://stackoverflow.com/questions/2049330/c-sharp-variable-scoping – Soner Gönül Nov 07 '14 at 15:03
  • This references C++ but it's the same issue: http://ericlippert.com/2014/09/25/confusing-errors-for-a-confusing-feature-part-one/ – Michael McGriff Nov 07 '14 at 15:08

0 Answers0