Code Fix

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

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

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

 1  class Program {
 2      static void Main(string[] args) {
 3          int[] nums = { 1, 2, 3 };
 4          System.Console.WriteLine(nums[_]);
                                          ^
 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 の原因と直し方

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

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

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