Skip to main content
2 of 2
deleted 248 characters in body
Robert Harvey
  • 200.1k
  • 55
  • 468
  • 679

Foreach loop and variable initialization

Is there a difference between these two versions of code?

foreach (var thing in things)
{
    int i = thing.number;
    // code using 'i'
    // pay no attention to the uselessness of 'i'
}

int i;
foreach (var thing in things)
{
    i = thing.number;
    // code using 'i'
}

Or does the compiler not care? When I'm speaking of difference I mean in terms of performance and memory usage. ..Or basically just any difference or do the two end up being the same code after compilation?

Alternatex
  • 1k
  • 2
  • 13
  • 24