Why does the following code compile? I would expect that it would complain about foo
not being declared in the second case branch. Does the compiler handle the declaration such that it's in scope for all cases?
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;
}
}
}
}