{"record":{"id":"5a697c2fe1d3af33","repo":"krahets/hello-algo","slug":"error-5a697c","errorCode":null,"errorMessage":"索引越界","messagePattern":"索引越界","errorType":"exception","errorClass":"IndexOutOfRangeException","httpStatus":null,"severity":"error","filePath":"codes/csharp/chapter_array_and_linkedlist/my_list.cs","lineNumber":35,"sourceCode":"    public MyList() {\n        arr = new int[arrCapacity];\n    }\n\n    /* 获取列表长度（当前元素数量）*/\n    public int Size() {\n        return arrSize;\n    }\n\n    /* 获取列表容量 */\n    public int Capacity() {\n        return arrCapacity;\n    }\n\n    /* 访问元素 */\n    public int Get(int index) {\n        // 索引如果越界，则抛出异常，下同\n        if (index < 0 || index >= arrSize)\n            throw new IndexOutOfRangeException(\"索引越界\");\n        return arr[index];\n    }\n\n    /* 更新元素 */\n    public void Set(int index, int num) {\n        if (index < 0 || index >= arrSize)\n            throw new IndexOutOfRangeException(\"索引越界\");\n        arr[index] = num;\n    }\n\n    /* 在尾部添加元素 */\n    public void Add(int num) {\n        // 元素数量超出容量时，触发扩容机制\n        if (arrSize == arrCapacity)\n            ExtendCapacity();\n        arr[arrSize] = num;\n        // 更新元素数量\n        arrSize++;","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/codes/csharp/chapter_array_and_linkedlist/my_list.cs#L17-L53","documentation":"IndexOutOfRangeException with message \"索引越界\" (index out of bounds) is thrown by Get(int index) on a custom dynamic-array MyList implementation when the supplied index is less than 0 or greater than or equal to arrSize (the current logical element count). It protects the backing arr[] from invalid access. The message is in Chinese, matching the source-language pedagogical convention.","triggerScenarios":"Calling Get(index) with index < 0 or index >= arrSize on a MyList instance. Most commonly a negative index, an index equal to arrSize (one-past-the-end), or any index computed from an off-by-one loop.","commonSituations":"Off-by-one errors in for-loops (e.g., iterating i <= Count() instead of i < Count()); passing a raw length as an index instead of length-1; assuming zero-based indexing end is Count() rather than Count()-1; passing user input directly as an index without validation.","solutions":["Validate the index against the list's Count() before calling Get(): require 0 <= index < Count().","Fix off-by-one loop bounds — use i < list.Count() when iterating indices.","If exposing an API, clamp or reject out-of-range input at the boundary before it reaches Get().","Add an assertion or precondition check in debug builds to catch invalid indices early."],"exampleFix":"// before\nint val = list.Get(i);\n\n// after\nif (i >= 0 && i < list.Count()) {\n    int val = list.Get(i);\n}","handlingStrategy":"validation","validationCode":"if (index >= 0 && index < list.Count()) {\n    int val = list.Get(index);\n}","typeGuard":"bool IsValidIndex(MyList list, int i) => i >= 0 && i < list.Count();","tryCatchPattern":"try { int val = list.Get(index); }\ncatch (IndexOutOfRangeException) { /* invalid index */ }","preventionTips":["Use i < Count() in loops, never i <= Count().","Validate index against the logical size, not capacity.","Re-validate after any Insert/Remove that changes Count()."],"tags":["array","dynamic-array","index-out-of-range","csharp","off-by-one"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}