初級 › 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
空欄を埋めてビルドを通す
配列の添字は0から始まり、末尾はLength-1です。3個の配列に5番目の要素はありません。
次の問題