Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么';这段代码不能编译吗?_C# - Fatal编程技术网

C# 为什么';这段代码不能编译吗?

C# 为什么';这段代码不能编译吗?,c#,C#,我不明白为什么这样的代码不能构建: if (SomeCondition) { Boolean x = true; } else { Boolean x = false; } Debug.WriteLine(x); // Line where error occurs 它会产生以下错误: 名称“x”在当前上下文中不存在 对我来说,x在所有情况下都是声明的,因为有一个else子句。那么,为什么编译器在Debug.WriteLine行上不知道它呢?这是由于变量的块作用域:{i

我不明白为什么这样的代码不能构建:

if (SomeCondition) {
    Boolean x = true;
} else {
    Boolean x = false;
}
Debug.WriteLine(x);     // Line where error occurs
它会产生以下错误:

名称“x”在当前上下文中不存在


对我来说,
x
在所有情况下都是声明的,因为有一个
else
子句。那么,为什么编译器在
Debug.WriteLine
行上不知道它呢?

这是由于变量的块作用域:
{int x=3;}
只在块内部可见。您应该将
x
的声明移到块外:

Boolean x;

if (SomeCondition) {
     x = true;
} else {
    x = false;
}
Debug.WriteLine(x);    
或者在上述情况下,甚至更好:

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

x超出了writeline的范围。

您当然可以简化为:

Boolean x = SomeCondition;
Debug.WriteLine(x);     // Line where error occurs
但是if不必在if语句之前声明变量,因此它仍然在if语句之后的范围内,如下所示:

Boolean x;    
if (SomeCondition) {
     x = true;
} else {
    x = false;
}
Debug.WriteLine(x);   

x
仅存在于其范围内,即
if
块或
else
块,而不是在
if
语句完成之后。不管执行哪个块,它都会被创建,但在到达调试语句之前,它也会在该块的末尾被销毁

if (SomeCondition) {
    Boolean x = true;       <-- created here
}                           <-- destroyed here
else
{
    Boolean x = false;      <-- created here
}                           <-- destroyed here

Debug.WriteLine(x);         <-- does not exist here

因此,它在
if
之前就已存在,并在之后继续存在。

变量仅在声明它的块中有效:

if (SomeCondition) { 
    Boolean x = true; 
    ...
    // end of life of Boolean x
} else { 
    Boolean x = false; 
    ...
    // end of life of Boolean x
}
当然,编译器可以对此进行推理

  • 如果一个变量在所有具有相同类型和名称的并行块中声明,那么它的可见性甚至会扩展到该块下方
……但他们为什么要这样做?它使编译器变得不必要的复杂,只是为了涵盖这一特殊情况


因此,如果要访问块外的
x
,则需要在块外声明它:

Boolean x;
if (SomeCondition) { 
    x = true; 
} else { 
    x = false; 
} 
...
// end of life of Boolean x
当然,在这种特殊情况下,更容易编写:

Boolean x = someCondition;

但我想这只是一个人为的例子来说明你的观点。

布尔x的定义只存在于定义的范围内。我注意到以下范围:

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
最好的方法是在外部声明变量:

Boolean x = false;
if (SomeCondition) {
    x = true;
}
else {
    x = false;
}
Debug.WriteLine(x); 
C#不是PHP。您必须在WriteLine的范围内声明它

你最好写:

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

if else块中定义的变量在其外部将不可用


我知道如何解决这个错误,谢谢。这不是我的问题:)这并没有回答问题,这是一个解决方案。@Ashburaczenko和其他人:我想你错过了我的忍者编辑:)事实上,这回答了问题。作者不理解else语句,在他的情况下,else语句是一行。变量的范围仅限于else块中的所有内容(同样是一行)。我想我们的samuraï注释比ninja编辑更快:)布尔x只存在于if和else语句的上下文中。这样做当然是错误的代码,会导致类似这样的问题,并且不是声明局部变量的“建议方法”。
Boolean x = SomeCondition;
Debug.WriteLine(x);