初級 › Index was outside the bounds of the array の原因と直し方 › パターン5
Index was outside the bounds of the array の原因と直し方
配列やリストの範囲外アクセスで実行時に発生します。
1 class Program { 2 static void Main(string[] args) { 3 int[] data = { 1, 2, 3, 4 }; 4 int idx = __; ^ 5 System.Console.WriteLine(data[idx]); 6 } 7 }
Unhandled exception. System.IndexOutOfRangeException: Index was outside the bounds of the array.
at Program.Main(String[] args) in Program.cs:line 5
空欄を埋めてビルドを通す
要素数4の配列で使える添字は0〜3までです。4は1つ超えています。
次の問題