简体   繁体   English

为什么此代码无法编译?

[英]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 名称“ x”在当前上下文中不存在

For me, x is declared in all cases because there is an else clause. 对我来说,在所有情况下都声明x ,因为存在else子句。 So why the compiler don't know it on the Debug.WriteLine line? 那么,为什么编译器在Debug.WriteLine行上不知道它?

x在写入行超出范围。

It is due to block scoping of variables: { int x = 3; } 这是由于变量的作用域限定: { int x = 3; } { int x = 3; } is only visible inside the block . { int x = 3; }在块内可见。 You should move the declaration of x outside the block: 您应该将x声明移到块外:

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: 因此,如果要在块外部访问x ,则需要在块外部声明它:

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. Boolean x的定义仅存在于所定义的范围内。 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. 在if-else块中定义的变量在其外部将不可用。

http://www.blackwasp.co.uk/CSharpVariableScopes.aspx 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: 但是如果不必在if语句之前声明变量,则它仍在if语句之后的范围内,如下所示:

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. x仅存在于其范围内,该范围是if块或else块, 而不是if语句完成之后。 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. 这样它就可以在if 之前存在并在之后进行。

C# is not PHP. C#不是PHP。 You MUST declare it in the scope of the WriteLine. 您必须在WriteLine的范围内声明它。

You better write: 你最好写:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM