Why doesn't this code compile?
I don't understand why such a code can't build:
if (SomeCondition) {
Boolean x = true;
} else {
Boolean x = false;
}
Debug.WriteLine(x); // Line where error oc开发者_JAVA百科curs
It creates the following error:
The name 'x' does not exist in the current context
For me, x
is declared in all cases because there is an else
clause. So why the compiler don't know it on the Debug.WriteLine
line?
x is out of scope at the writeline.
It is due to block scoping of variables: { int x = 3; }
is only visible inside the block. You should move the declaration of x
outside the block:
Boolean x;
if (SomeCondition) {
x = true;
} else {
x = false;
}
Debug.WriteLine(x);
Or in the above case, even better:
bool x = SomeCondition;
Debug.WriteLine(x);
A variable is only valid in the block in which it was declared:
if (SomeCondition) {
Boolean x = true;
...
// end of life of Boolean x
} else {
Boolean x = false;
...
// end of life of Boolean x
}
Of course, the compiler could reason that
- if a variable is declared in all parallel blocks with the same type and the same name, then it's visibility extends even below that block...
...but why should they do that? It makes the compiler unnecessarily complicated, just to cover this one special case.
Thus, if you want to access x
outside of your blocks, you need to declare it outside of the blocks:
Boolean x;
if (SomeCondition) {
x = true;
} else {
x = false;
}
...
// end of life of Boolean x
Of course, in this special case, it's much easier to write:
Boolean x = someCondition;
but I guess this was just a contrived example to illustrate your point.
The definition of Boolean x
only exists within the scope that it is defined. I have noted the scopes below:
if (SomeCondition) { //new scope
Boolean x = true;
} // x is not defined after this point
else { //new scope
Boolean x = false;
} // x is not defined after this point
Debug.WriteLine(x); // Line where error occurs
The best way is to declare the variable outside :
Boolean x = false;
if (SomeCondition) {
x = true;
}
else {
x = false;
}
Debug.WriteLine(x);
A variable defined within the if-else block will not be available outside it.
http://www.blackwasp.co.uk/CSharpVariableScopes.aspx
You can of course simplify to:
Boolean x = SomeCondition;
Debug.WriteLine(x); // Line where error occurs
but if not have to declare the variable before the if statement so it's still in scope after the if statement like this:
Boolean x;
if (SomeCondition) {
x = true;
} else {
x = false;
}
Debug.WriteLine(x);
x
only exists within its scope, which is either the if
block or the else
block, not after the if
statement has finished. While it's created no matter which block gets executed, it's also destroyed at the end of that block before you get to the debug statement.
if (SomeCondition) {
Boolean x = true; <-- created here
} <-- destroyed here
else
{
Boolean x = false; <-- created here
} <-- destroyed here
Debug.WriteLine(x); <-- does not exist here
You can change it to something like:
Boolean x;
if (SomeCondition) {
x = true;
} else {
x = false;
}
Debug.WriteLine(x);
so that it is bought into existence before the if
and carries on afterwards.
C# is not PHP. You MUST declare it in the scope of the WriteLine.
You better write:
Boolean x = SomeCondition;
Debug.WriteLine(x);
精彩评论