Hi ho,
I've encountered a weird thing with C#. Apparently when you do something like this:
private void MyMethod()
{
for (int i = 0; i < 10; i++)
{
}
int i = 5; // this gives an error...
}
This is just extremely weird and annoying. According to MS this is to protect the programmer, for example when he copy/paste a piece of code. But that's just ridiculous, a variable declared in the scope of the for loop should stay there, and get disposed when the loop ends. And even weirder is that you can still do this:
private void MyMethod()
{
for (int i = 0; i < 10; i++)
{
}
for (int i = 0; i < 10; i++) // shouldn't this give an error then? no it doesn't, don't ask me why...
{
}
}
Why does this compile then? If MS wants i to have the scope of the whole method, why am I still able then to declare it in a second for loop, but not in a normal way?
Thanks in advance,
Jitse
Loading
JitsePosted Nov 18, 2007, 8:54 AM
private void MyMethod()
{
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
}
}
{
int x, y;
}
}
Which doesn't look nice, but this way a bypass at least exists. Of course I'm not going to do that, I'll just rename them to xPos and yPos or so. :)
Thanks for the replies.
AlanPosted Nov 17, 2007, 6:55 PM
The reason why the second example in your original post compiles is because the scopes of the two 'i' variables are disjoint i.e. each 'i' variable is only scoped to its enclosing for statement and so there's no overlap.
In the first example the scope of the outer 'i' variable consists technically of the whole method block which, of course, includes the for statement even though it's not declared until after the for statement has ended. The scopes of the 'i' variables therefore overlap and so it's not allowed.
The alternative to this state of afairs would have been to allow an inner loop variable to 'hide' an outer local variable with the same name. However, experience of other languages where this is allowed suggested that it can lead to subtle or hard to find bugs if the programmer forgets which variable he or she is referring to. Moreover, the risk of this happening increases when code is rearranged or pasted in. The C# design team therefore decided not to allow name hiding in this situation.
However, they did allow name hiding in other situations such as where a local variable hides a field with the same name and so they haven't been entirely consistent here.
Personally, I dislike name hiding anyway and always try to give all my variables distinct names.
JitsePosted Nov 17, 2007, 4:26 PM
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
}
}
And then after that I had to do some non-loop things which also had an x & y, so I declared them, which caused the error.
But it gets more annoying when you're having it with other variables than just the variable used by the for loop. Look at this for example:
while (true)
{
int myVar = 5;
break;
}
int myVar = 18; // error
This just messes up the nice decent scope structure that a normal and modern programming language should have. I don't know any other programming language having this ignorance of general programming structures...
DanPosted Nov 17, 2007, 4:15 PM