2

As for Java refugee this code is perfectly fine. The int i lives in the loop only and the second int i causes no conflict. So there should be no problem. Altough, This code in C# causes i variable ambiguity.

static void Main(String[] args)
{
    for (int i = 0; i < 10; i++)
    {
    }
    int i = 0;
    while (i < 10) { i++; }
}

Thus(beign naive) this code should be valid

static void Main(String[] args)
{
    for (int i = 0; i < 10; i++)
    {
    }
    i++;
}

but it says the i variable is not declared which as expected is true. Finally only nesting second int i in the block fixes the ambiguity:

static void Main(String[] args)
{
    for (int i = 0; i < 10; i++)
    {
    }
    {
        int i = 0;
        while (i < 10) { i++; }
    }
}

The question is: what rule/mechanism of C# language causes variable name ambiguity in the first example given?

The printscreen from VS2013:

enter image description here

Rufus L
  • 36,127
  • 5
  • 30
  • 43
Yoda
  • 17,363
  • 67
  • 204
  • 344
  • Side-note: Eric Lippert has an interesting series of blog posts regarding that error message and its friends, [here](http://ericlippert.com/2014/09/25/confusing-errors-for-a-confusing-feature-part-one/). – Blorgbeard Mar 05 '15 at 00:15
  • Duplicate contains answer by Eric Lippert who goes into all details (and cover `for` blocks) - http://stackoverflow.com/a/2050864/477420. – Alexei Levenkov Mar 05 '15 at 00:15
  • @AlexeiLevenkov: ??? :-) – T.J. Crowder Mar 05 '15 at 00:17

0 Answers0