初級 › not all code paths return a value の原因と直し方 › パターン2
not all code paths return a value の原因と直し方
値を返すと宣言したメソッドで、すべての実行経路にreturn文があるかをコンパイラが検証します。
1 class Program { 2 static bool IsPositive(int n) { 3 if (n > 0) { 4 return true; 5 } 6 _____________ ^ 7 } 8 static void Main(string[] args) { 9 System.Console.WriteLine(IsPositive(-1)); 10 } 11 }
Program.cs(2,17): error CS0161: 'Program.IsPositive(int)': not all code paths return a value
空欄を埋めてビルドを通す
if分岐にしかreturnがありません。elseに相当する経路にもreturnが必要です。
次の問題