为什么下面的代码会编译?我希望它会抱怨foo
没有在第二个案例分支中声明。编译器是否处理声明以使其适用于所有情况?
using System;
namespace Scratch
{
class Scratch
{
public static void Main()
{
var x = 2;
switch (x)
{
case 1:
var foo = "one";
Console.Out.WriteLine(foo);
break;
case 2:
foo = "two"; // is foo in scope here?
Console.Out.WriteLine(foo);
break;
}
}
}
}