Back to questions
JuniorTheoryVery common
What happens when this program is compiled and run?
string cmd = "start";
switch (cmd)
{
case "start":
Console.Write("A");
case "stop":
Console.Write("B");
break;
}It does not compile
- Expecting
AB: in C# acasebranch cannot run on into the next one, and a branch withoutbreakstops compilation withCS0163. - Expecting the compiler to add the
breakitself and printA: it adds nothing and reports an error instead.
- How would you fix the program so that it prints
A? - When may two
caselabels stand one under the other with nobreakbetween them?