Otiel
Otiel

Reputation: 18743

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 occurs

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?

Upvotes: 0

Views: 367

Answers (8)

Jayanta Dey
Jayanta Dey

Reputation: 307

A variable defined within the if-else block will not be available outside it.

http://www.blackwasp.co.uk/CSharpVariableScopes.aspx

Upvotes: 2

marcocamejo
marcocamejo

Reputation: 828

C# is not PHP. You MUST declare it in the scope of the WriteLine.

You better write:

Boolean x = SomeCondition;
Debug.WriteLine(x);

Upvotes: 0

Foo Bah
Foo Bah

Reputation: 26281

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); 

Upvotes: 2

Heinzi
Heinzi

Reputation: 172448

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.

Upvotes: 4

paxdiablo
paxdiablo

Reputation: 882396

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.

Upvotes: 1

Chris Snowden
Chris Snowden

Reputation: 5002

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);   

Upvotes: 1

Eric
Eric

Reputation: 6436

x is out of scope at the writeline.

Upvotes: 8

sehe
sehe

Reputation: 393759

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);

Upvotes: 5

Related Questions