Question

Possible Duplicate:
Child Scope & CS0136
C# Variable Scoping

Though I have been using C# for quite some time, I have just stumbled upon this error.

If I have the following:

if(true)
{
    int x = 0;
}
int x = 0;

I get an error that says: A local variable named 'x' cannot be declared in this scope because it would give a different meaning to 'x', which is already used in a child scope to denote something else.

And if I do this:

if(true)
{
    int x = 0;
} 
x = 0;

I get an error that says: The name 'x' does not exist in the current context.

I can understand having one or the other, but why do both of these errors exist? Is there a way around the first option? I find it very annoying.

Thanks.

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top