初級 › Index was outside the bounds of the array の原因と直し方 › パターン3
Index was outside the bounds of the array の原因と直し方
配列やリストの範囲外アクセスで実行時に発生します。
1 class Program { 2 static void Main(string[] args) { 3 int[] nums = { 1, 2, 3 }; 4 int idx = __; ^ 5 System.Console.WriteLine(nums[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
空欄を埋めてビルドを通す
C#の配列は負のインデックスを末尾からの参照として扱いません(Pythonとは違います)。
次の問題