CS.SWITCH.DEFAULT.POSITION
Default label does not appear as the first or the last label in switch statement.
The default label is not at the beginning or end of a switch statement.
Vulnerability and risk
If the default label is not placed at the beginning or end of a switch statement, maintainability may be impaired, or incorrect coding may cause unintended behavior.
Mitigation and prevention
Place the default label at the beginning or end of a switch statement.
Example
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace kmcustom 8 { 9 class C03 10 { 11 /* default located at first */ 12 public void testOK1(int num) 13 { 14 switch (num) 15 { 16 default: //OK 17 break; 18 case 1: 19 break; 20 case 2: 21 break; 22 23 } 24 } 25 26 /* default located at last */ 27 public void testOK2(int num) 28 { 29 30 switch (num) 31 { 32 case 1: 33 break; 34 case 2: 35 break; 36 default: //OK 37 break; 38 } 39 } 40 41 42 /* default located in the middle */ 43 public void testNG1(int num) 44 { 45 switch (num) 46 { 47 case 1: 48 break; 49 default://NG 50 break; 51 case 2: 52 break; 53 } 54 55 } 56 57 /* no default */ 58 public void testNG2(int num) 59 { 60 switch (num) //NG 61 { 62 case 1: 63 break; 64 case 2: 65 break; 66 } 67 68 } 69 70 } 71 }