Code Fix

初級 › Index was outside the bounds of the array の原因と直し方 › パターン4

Index was outside the bounds of the array の原因と直し方

配列やリストの範囲外アクセスで実行時に発生します。

 1  class Program {
 2      static void Main(string[] args) {
 3          int[] vals = { 4, 8, 15 };
 4          System.Console.WriteLine(vals[__]);
                                           ^
 5      }
 6  }
Unhandled exception. System.IndexOutOfRangeException: Index was outside the bounds of the array. at Program.Main(String[] args) in Program.cs:line 4

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

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

Index was outside the bounds of the array の原因と直し方

ループの境界条件の誤りが典型的な原因です。

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

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