上級 › SwitchExpressionException の原因と直し方 › パターン5
SwitchExpressionException の原因と直し方
switch式が全てのパターンを網羅していないとき、コンパイラは警告を出しますが止めません。
1 class Program { 2 static string Size(int n) { 3 return n switch { 4 1 => "S", 5 2 => "M", 6 3 => "L", 7 }; 8 } 9 static void Main(string[] args) { 10 System.Console.WriteLine(Size(__)); ^ 11 } 12 }
Unhandled exception. System.Runtime.CompilerServices.SwitchExpressionException: Non-exhaustive switch expression failed to match its input.
at Program.Size(Int32 n) in Program.cs:line 3
空欄を埋めてビルドを通す
サイズ表現も同様です。想定していない値をコンパイラは検出できません。
次の問題