C#有效语法(IF语句中没有括号的变量声明)

cla*_*oda 0 c# syntax compiler-errors

我有一个简单的问题,我似乎无法理解.

为什么这个有效的c#:

if (node != null)
{
     string fullAddress = node.InnerText;
}
Run Code Online (Sandbox Code Playgroud)

这不是吗?

if (node != null)
     string fullAddress = node.InnerText;
Run Code Online (Sandbox Code Playgroud)

这是编译器中的错误还是这个?

Rob*_*vey 7

因为您定义的是没有封闭范围的局部变量.


Ani*_*dha 5

msdn说这个

声明语句在块中是允许的,但不允许作为嵌入语句.