Same actions for many cases in a switch

Sometimes you would like to use a switch block in which many cases must execute the same actions. For this purpose, you don’t need to duplicate the code for each case. You may just use the following syntax:


switch (myValue)
{
case myFirstCaseValue:
case mySecondCaseValue:
Actions();
break;
case myThirdAction:
OtherActions();
break;
default:
break;
}

As you can see, you just have to group the cases together (without any break between them of course).

Advertisements