初級 › switch文のフォールスルー禁止エラーの原因と直し方 › パターン3
switch文のフォールスルー禁止エラーの原因と直し方
C#のswitch文はCやJavaと違い、暗黙のフォールスルーを認めません。
1 class Program { 2 static void Main(string[] args) { 3 int score = 2; 4 switch (score) { 5 case 1: 6 System.Console.WriteLine("low"); 7 break; 8 case 2: 9 System.Console.WriteLine("mid"); 10 _________ ^ 11 case 3: 12 System.Console.WriteLine("high"); 13 break; 14 } 15 } 16 }
Program.cs(8,13): error CS0163: Control cannot fall through from one case label ('case 2:') to another
空欄を埋めてビルドを通す
2番目のcaseにもbreakが要ります。どのcaseも独立して終える必要があります。
次の問題