Possible Compiler Bug (C# 3.0)

T

Tomasz J

The attached program does not compile. The error message says:
"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"
- which does not make much sense. Is it a compiles bug?
TJ

class Program
{
static void Main(string[] args)
{
switch (0) {
case 0:
for (int i = 0; ; ) {
}
case 1:
// error CS0136: 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
int i = 0;
}
}
}
 
J

JPK

e-gads ! I thought the days of x,y,z,j,k,i were dead and gone. Should we
call this style 'retro-code'? ;-)
 
S

sloan

Tomasz J said:
The attached program does not compile. The error message says:
"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"
- which does not make much sense. Is it a compiles bug?
TJ

class Program
{
static void Main(string[] args)
{
switch (0) {
case 0:
for (int i = 0; ; ) {
}
case 1:

Console.Writeline( i.ToString());
//if the above compiles, and it shows the "i" value, then it is
IN_SCOPE and already declared.
 
T

Tomasz J

yes, you definitely should

JPK said:
e-gads ! I thought the days of x,y,z,j,k,i were dead and gone. Should we
call this style 'retro-code'? ;-)



Tomasz J said:
The attached program does not compile. The error message says:
"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"
- which does not make much sense. Is it a compiles bug?
TJ

class Program
{
static void Main(string[] args)
{
switch (0) {
case 0:
for (int i = 0; ; ) {
}
case 1:
// error CS0136: 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
int i = 0;
}
}
}
 
M

Mythran

JPK said:
e-gads ! I thought the days of x,y,z,j,k,i were dead and gone. Should we
call this style 'retro-code'? ;-)
<snip>

Heck no, I still use i, j, k, x, y, and z variables because when developers
see those, they usually understand that they represent a counter in the
enclosing block (or on of it's ancestor blocks). To take a single counter,
'i', and name it 'thisIsMyCounter' is IMHO, just plain rude lol. Why force
someone to type all that out when all it is is a counter?

HTH,
Mythran
 
A

Alberto Poblacion

Tomasz J said:
The attached program does not compile. The error message says:
"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"
- which does not make much sense. Is it a compiles bug?
TJ

class Program
{
static void Main(string[] args)
{
switch (0) {
case 0:
for (int i = 0; ; ) {
}
case 1:
// error CS0136: 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
int i = 0;
}
}
}

It's not a bug, it's a documented feature. Contrary to other "flavors" of
C, in C# it is not allowed to have a variable in a nested block named the
same as a variable in its containing block. For instance, the following
would compile in traditional C, but not in C#:

while (condition1)
{
int i;
...
while (condition2)
{
int i;
...
}
}

The reasoning is that this used to lead to many programming errors. If
you access "i" in the inner block, it refers to the "i" that is declared
inside that same block, but in a larger program it is easy to make a mistake
and think that it is referring to the outer "i". To avoid this risk, the C#
designers decided to disallow this situation.
 
J

JPK

If Collection contains ;

Cars -> "CarCounter"
Animals -> "AnimalCounter"
People -> "PersonCounter"


When nesting multiple Scopes it can be hard to recognize which scope you
are in when all you have if i,j,k. If I see "CarCounter" I know exactly
what scope I am in .



JIM
 
C

Chakravarthy

It's really funny.. my app got compiled and I got only warning as

"Warning 1 The variable 'i' is assigned but its value is never usedD:\Pract\FirstConsoleApp\FirstConsoleApp\Program.cs 13 30 FirstConsoleApp"But when I run the app, it is endup with infinite loop.

What about others.. did any one tried compiling and running the app?
 
J

Jon Skeet [C# MVP]

It's really funny.. my app got compiled and I got only warning as

"Warning   1       The variable 'i' is assigned but its value is never usedD:\Pract\FirstConsoleApp\FirstConsoleApp\Program.cs     13      30      FirstConsoleApp"But when I run the app, it is endup with infinite loop.

What about others.. did any one tried compiling and running the app?

My guess is that you're using Mono. (Mind you, in Mono I get 3
warnings, not just one.) The fact that it compiles under mcs is a bug
in mcs, in my view. The spec's pretty clear on this front, I think.

(And yes, if you run it it certainly will give an infinite loop due to
the nature of the for loop.)

Jon
 
A

Arne Vajhøj

Mythran said:
Heck no, I still use i, j, k, x, y, and z variables because when
developers see those, they usually understand that they represent a
counter in the enclosing block (or on of it's ancestor blocks). To take
a single counter, 'i', and name it 'thisIsMyCounter' is IMHO, just plain
rude lol. Why force someone to type all that out when all it is is a
counter?

If it actually made the code more readable then it would be worth to
force them.

But the fact is that i,j,k has become a standard for for loop
counters.

So even though the variable names are short they actually tell
the reader a lot.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top