ECMA-334 C# Language Specification14.5.2.1: Invariant meaning in blocks |
For each occurrence of a given identifier as a simple-name in an expression, every other occurrence of the same identifier as a simple-name in an expression within the immediately enclosing block (15.2) or switch-block (15.7.2) must refer to the same entity. This rule ensures that the meaning of a name in the context of an expression is always the same within a block.
The example
class Test
{
double x;
void F(bool b) {
x = 1.0;
if (b) {
int x = 1;
}
}
}
|
class Test
{
double x;
void F(bool b) {
if (b) {
x = 1.0;
}
else {
int x = 1;
}
}
}
|
Note that the rule of invariant meaning applies only to simple names. It is perfectly valid for the same identifier to have one meaning as a simple name and another meaning as right operand of a member access (14.5.4).
struct Point
{
int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}