Skip to main content
gamedev.net
🔒 Locked

[.net] [C#]Simple variable scope problem

Started by g0nzo Jan 3, 2007 at 6:24 AM 3 replies 1k views
Original Post
g0nzo
g0nzo
Hi, I've got a for loop:

for (int i = 0; i < steps; i++)
{
  // use i
}
The problem is that if I try to declare variable 'i' after this loop (int i;) I get an error: A local variable named 'i' cannot be declared in this scope because it would give a different meaning to 'i', which is already used in a 'child' scope to denote something else. If I try to use variable 'i' after this loop (i.e. i++;) compiler says that: The name 'i' does not exist in the current context. So there's no variable 'i' after the loop anymore, yet I can't create it? Why is that?
benryves
benryves
When you put int i; outside the for loop, whether it is above or below it it is still declared in the scope outside the for which causes problems with the i nested within the scope of the for loop.

The solution would be to use:

int i;for (i = 0; i < steps; i++) { // Note removal of 'int'    // use i}// use i
[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]
remigius
remigius
Coming from Java I found this behavior quite puzzling too at first, but apparently it's by design as per section 8.5.1 of the C# language spec. According to the specifcation, the variable i exists in the entire enclosing scope in which it is defined, including child scopes.

Effectively this means that...

void SomeFunction(){  int i = 0;  for(int i = 0; i < 1; i++) {}}


...and...

void SomeFunction(){  for(int i = 0; i < 1; i++) {}  int i = 0;}


...are kinda equivalent in that they are illegal/incorrect redefinitions of the same variable. That the former case is incorrect should not be surprising, since you can't redeclare int i in the for loop. The C# spec however dictates that int i the latter snippet exists in the entire scope of SomeFunction(), so it's pretty much incorrect for the same reason as the first snippet is.

This decission was made to prevent unintended re-use of existing variables, as described here on MSDN. For some speculation, I'd guess that this decission also might make optimization easier on the C# compiler, since variables in a scope and its child scopes will be uniquely named, so the compiler can move around local scoped instructions and local child scopes as it sees fit.
Iftah
Iftah
gonzo - I believe this is a choice they made on purpose to disallow bad programming habits,
in this case using same name to different variables in the same function. Even if possible for the compiler to understand what you mean, it is a bad habit as it makes code less readable to humans and/or may cause hard to find bugs.
g0nzo
g0nzo
Thank you for solution and explanation!

I tried removing int from the for loop before, but it didn't work. I took a closer look at the code and I had another for loop hidden in a #region. Now it works correctly and I've learnt something new [smile]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.