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

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


Is there something that

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-18 00:34

    move the string declaration to before the

    switch(value)
    

    statement. Then assign x for each case.

提交回复
热议问题