Nonplussed by scoping - any explanations?

D

Diane Selby

Hi-

I am confused by C#'s scoping of variables within a child scope (for
loop, conditional, block of code), and was hoping someone could
explain. Here's the offending code:

for (int i=0; i<10; i++)
{
/////
}
int i = 100;

The compiler complains:
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

This doesn't make sense. The variable i defined in the for loop
should go out of scope after the closing curly brace, and it should be
OK to define a new variable which just happens to have the same name,
right?

If i is still defined after the for block, this should work fine:

for (int i=0; i<10; i++)
{
/////
}

i = 100;

But this doesn't work either:
The name 'i' does ot exist in the class or namespace 'Dooku.kudu.bobo'

So it looks to me like i is neither in scope or out of scope after the
for loop. It's in limbo!

Any ideas?

Thanks,
Diane.

P.S. THis is not just a quirk of the for loop. THe behavior is the
same with conditionals and just plain old {} blocks...
 
E

Erik Frey

Diane Selby said:
Hi-

I am confused by C#'s scoping of variables within a child scope (for
loop, conditional, block of code), and was hoping someone could
explain. Here's the offending code:

for (int i=0; i<10; i++)
{
/////
}
int i = 100;
<snip>

Diane,

The reason this doesn't work is because although C# lets you define a
variable anywhere within a block, its scope still applies to the whole
block. It may APPEAR that you've defined the variable halfway through the
block, and the scope only applies from that line forward, but really it got
allocated on the stack as soon as you entered the block. In standard C, it
was enforced that you always had to declare all your variables at the
beginning of a block. Personally I prefer the standard C way.

But keep in mind that even though variable declarations apply to the entire
block, the compiler will squawk at you if you try to do this:

{
i = 10;
int i;
}

As a style guideline, if you follow the practice of always declaring your
variables at the beginning of your code blocks, you'll never run into
problems like the one you've stated.

Erik
 
J

Jon Skeet [C# MVP]

Diane Selby said:
I am confused by C#'s scoping of variables within a child scope (for
loop, conditional, block of code), and was hoping someone could
explain. Here's the offending code:

for (int i=0; i<10; i++)
{
/////
}
int i = 100;

The compiler complains:
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

This doesn't make sense. The variable i defined in the for loop
should go out of scope after the closing curly brace, and it should be
OK to define a new variable which just happens to have the same name,
right?

Wrong.

From the C# spec:

<quote>
The scope of a local variable declared in a local-variable-declaration
(§15.5.1) is the block in which the declaration occurs.
</quote>

In other words, your second variable "i" is in *scope* (although not
actually available for use) *before* the for loop.
 

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