C# switch variable initialization: Why does this code NOT cause a compiler error or a runtime error?

后端 未结 6 1382
粉色の甜心
粉色の甜心 2020-12-17 23:46
...
case 1:
   string x = \"SomeString\";
   ...
   break;
case 2:
   x = \"SomeOtherString\";
   ...
   break;
...


Is there something that

6条回答
  •  旧时难觅i
    2020-12-18 00:25

    It looks like the scoping of variables is within the switch, not the case, probably because cases can be stacked. Notice if you try to reference x outside of the switch it will fail.

提交回复
热议问题