Switch Statement Fallthrough in C#

Switch statement fallthrough in C#?

(Copy/paste of an answer I provided elsewhere)

Falling through switch-cases can be achieved by having no code in a case (see case 0), or using the special goto case (see case 1) or goto default (see case 2) forms:

switch (/*...*/) {
case 0: // shares the exact same code as case 1
case 1:
// do something
goto case 2;
case 2:
// do something else
goto default;
default:
// do something entirely different
break;
}

Using goto to fall-through in switch when also switching on type in C#

If my understanding is correct then you can achieve the same with only 3 lines of code instead of switch and goto statements:

foreach (Animal animal in listOfAnimals)
{
if (animal is Dog dog) dog.Bark();
if (animal is Mammal mammal) mammal.DoMammalStuff();
animal.Eat();
}

UPDATE #1: Use switch statement

In C# you can call break, return, continue, throw or goto to control the fall-through.

goto has a really special version where you can jump to a case x rather than a predefined label x. But there is one caveat: the x must be compile time constant.

So, if you stick to use switch statement then you can do one of the following:

int type = animal switch
{
Dog dog => 3,
Mammal mammal => 2,
_ => 1
};

switch (type)
{
case 3:
(animal as Dog).Bark();
goto case 2;
case 2:
(animal as Mammal).DoMammalStuff();
goto case 1;
case 1:
animal.Eat();
break;
}

OR

ImmutableDictionary<Type, int> mapping = new Dictionary<Type, int>
{
{ typeof(Dog), 3 },
{ typeof(Mammal), 2 }
}.ToImmutableDictionary();

int type = mapping.TryGetValue(animal.GetType(), out type) ? type : 1;

switch (type)
{
case 3:
(animal as Dog).Bark();
goto case 2;
case 2:
(animal as Mammal).DoMammalStuff();
goto case 1;
case 1:
animal.Eat();
break;
}

Extra condition in switch case (Fallthrough?)

I tried a solution with goto and it actually works. Thank you for your answers:


public void myFunction(int value)
{
switch (value)
{
case 0:
//Stuff A
break;

case 1:
//Stuff B
break;

case 2:
case 3:
//Stuff C
break;

case 4: //Requires "Extra stuff" and "Stuff C"
//Extra stuff for case 4
goto case 3;

//another possible cases...
case 5:
//etc
break;
}
}

Please notice that this solution will only work in cases that do NOT require a concrete order in the execution of the statements.

If for example in this case "Extra stuff" must be executed after "Stuff C" the correct way to do it is using an If statement inside case 3.

Is there a way to make my switch/case fall through to the next case in C#?

You need to add a break statement even if it's the last case:

switch (myCurrentVersion)
{
case null:
case "":
case "0":
UpdateToV1();
goto case "1";
case "1":
UpdateToV2();
break;
}

Why do fall through Switch cases compile in c#

First off, the code you provided doesn't throw a run time error. Secondly, it falls under a different category (from same MSDN article, emphasis mine):

A switch statement can include any number of switch sections, and each
section can have one or more case labels (as shown in the string case
labels example below). However, no two case labels may contain the
same constant value.

The difference is whether you have multiple empty case statements, that's allowed. But you can't have a case with code in it, and let it fall through.

Switch statement fall-through...should it be allowed?

It may depend on what you consider fallthrough. I'm ok with this sort of thing:

switch (value)
{
case 0:
result = ZERO_DIGIT;
break;

case 1:
case 3:
case 5:
case 7:
case 9:
result = ODD_DIGIT;
break;

case 2:
case 4:
case 6:
case 8:
result = EVEN_DIGIT;
break;
}

But if you have a case label followed by code that falls through to another case label, I'd pretty much always consider that evil. Perhaps moving the common code to a function and calling from both places would be a better idea.

And please note that I use the C++ FAQ definition of "evil"

Switch Fall Through Not Working as Expected

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.



Related Topics



Leave a reply



Submit