Code Fix

中級ArgumentOutOfRangeException の原因と直し方 › パターン2

ArgumentOutOfRangeException の原因と直し方

List<T>のインデクサに範囲外の値を渡すと発生します。

 1  class Program {
 2      static void Main(string[] args) {
 3          var nums = new System.Collections.Generic.List<int> { 1, 2, 3 };
 4          int idx = __;
                       ^
 5          System.Console.WriteLine(nums[idx]);
 6      }
 7  }
Unhandled exception. System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index') at Program.Main(String[] args) in Program.cs:line 5

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

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

ArgumentOutOfRangeException の原因と直し方

配列のIndexOutOfRangeExceptionとは異なる例外クラスです。

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

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