在C#中,您可以在未附加到任何其他语句的方法中创建块.
public void TestMethod()
{
{
string x = "test";
string y = x;
{
int z = 42;
int zz = z;
}
}
}
Run Code Online (Sandbox Code Playgroud)
此代码编译并运行,就像主方法中的大括号不存在一样.还要注意块内部的块.
有没有这种情况有价值?我还没有找到,但我很想知道其他人的发现.