[ Switch Fall Through Not Working as Expected ]
I have a switch block and it is not behaving as I would expect it to. As I've looked through similar questions on this forum, the answers don't directly address my issue, but seem to confirm my thinking. Please tell me where I'm going wrong. Also, I know I can accomplish this another, and probably better way, but that's not what I'm asking. I want to know where my understanding of fall-through is faulty.
switch (ncPointType)
{
case "MSD":
adjustDisabled = LastToken(initLine, adjustDisabled);//fall through intentional
case "MSI":
case "BI":
latchingPoint = FirstToken(initLine, latchingPoint);
break;
Now, per my understanding, if ncPointType == "MSD", adjustDisabled and latchingPoint should set. If "MSI", latchingPoint should be set. But the compiler flags the first "case" with the error "Control cannot fall through from one case label ('case "MSD":') to another. Why is this code not valid?
Answer 1
In C# you must explicitly leave the case
section in question. You can use goto case "MSI";
in the end of the first section.
Of course a section of a switch
block can also end with break
, return
, throw
, an infinite loop (that the C# compiler can determine is infinite) and so on.