Code Fix

初級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

空欄を埋めてビルドを通す

広告
広告スロット(未設定)

not all code paths return a value の原因と直し方

このエラーの解説と類題をまとめて見る →

広告
広告スロット(未設定)