Alex Rivera | Logout

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

Asked 2009-05-14T15:51:02.903
10
...
case 1:
   string x = "SomeString";
   ...
   break;
case 2:
   x = "SomeOtherString";
   ...
   break;
...


Is there something that I am not understanding about the switch statement in C#? Why would this not be an error when case 2 is used?
Edit: This code works and doesn't throw an error.

Edit
Report

1 Answer

-2

move the string declaration to before the

switch(value)

statement. Then assign x for each case.

answered 2009-05-14T15:52:59.500

Your Answer