scope question :

  • Thread starter Thread starter NotYetaNurd
  • Start date Start date
Agreed, no argument

Can you just read the link I gave and made your comments?

Nirosh.

Champika Nirosh said:
What proof you have to say that the the variable (which define in a block)
lifetime is not equal to that of the entire procedure.

your example is not capable of talking about the life time and I am not
talking about the SCOPE.

Okay, I think it's time to start looking at what the spec has to say.
ECMA numbering used throughout.

From section 12.1.7:

<quote>
The lifetime of a local variable is the portion of program execution
during which storage is guaranteed to be reserved for it. This lifetime
extends from entry into the block, for-statement, switch-statement, or
using-statement with which it is associated, until execution of that
block, for-statement, switch-statement, or using-statement ends in any
way.
</quote>

So no, not the whole method - just the block it's associated with.

Also from 12.1.7:

<quote>
Within the scope of a local variable, it is a compile-time error to
refer to that local variable in a textual position that precedes its
local-variable-declarator.
</quote>

That addresses your other question about why you can't do

i=1;
int i;

From section 10.7:

<quote>
The scope of a local variable declared in a local-variable-declaration
(§15.5.1) is the block in which the declaration occurs.
The scope of a local variable declared in a switch-block of a switch
statement (§15.7.2) is the switch-block.
The scope of a local variable declared in a for-initializer of a for
statement (§15.8.3) is the for-initializer, the for-condition, the for-
iterator, and the contained statement of the for statement.
</quote>
 
Champika Nirosh said:
No I don't think since it is .Net. Rules that are use in CLR are almost same
for both C# and VB.

Rules of scope for a variable are *language* rules, not CLR rules.
 
Champika Nirosh said:
Agreed, no argument

Can you just read the link I gave and made your comments?

I'm not sure I see the point, to be honest - that's VB.NET, which could
have entirely different scoping rules. Just because they both target
..NET doesn't mean things have to work exactly the same way.

For instance, you could have a language D# which *did* allow

i=5;
int i;

Why would that have to change what code is generated? The D# compiler
could just rearrange the variable declaration to the start of its
scope, and then call the C# compiler. They would both target .NET, but
would have different rules for how variables could be accessed.
 
Champika,
for (int i=0; i<6; i++) === int i; for(i=0; i<6; i+)
That doesn't sound correct, as I can do:

for(int i=0;i<6;i++)
{

}
for(long i=0;i<6;i++)
{

}

Which does compile & run.

By your statement the following would be equal, which actually causes a
compile error!

int i; for(i=0;i<6;i++)
{

}
long i; for(i=0;i<6;i++)
{

}

As i is already defined to be an int!

I will defer to one of the C# experts as to the "why"!

Hope this helps
Jay
 
Back
Top