{"record":{"id":"68a1f1d665726584","repo":"krahets/hello-algo","slug":"error-68a1f1","errorCode":null,"errorMessage":"索引越界","messagePattern":"索引越界","errorType":"exception","errorClass":"IndexOutOfRangeException","httpStatus":null,"severity":"error","filePath":"zh-hant/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/zh-hant/codes/csharp/chapter_array_and_linkedlist/my_list.cs#L17-L53","documentation":"Thrown by my_list.Get(int index) (zh-hant/codes/csharp/chapter_array_and_linkedlist/my_list.cs:35) as System.IndexOutOfRangeException with message \"索引越界\" (\"index out of range\") when index is outside [0, arrSize). This is the correct, specific exception type for a bounds violation in a list-like container. Get reads 'arr[index]', so the guard protects the backing array.","triggerScenarios":"Calling Get(index) where index < 0 or index >= arrSize — e.g. Get(arrSize), Get(-1), or Get on an index computed from an off-by-one loop bound.","commonSituations":"Looping 'for (i=0; i<=arrSize; i++)' (<= instead of <); reading the slot at the logical end (arrSize) instead of arrSize-1; passing an externally computed index without clamping; forgetting that arrCapacity != arrSize.","solutions":["Validate '0 <= index < list.Size()' before calling Get.","Fix loop bounds to '< list.Size()' rather than '<='.","Use the list's own Size()/Capacity() rather than the backing array length when computing indices.","Add a TryGet(int, out int) helper or clamp the index if out-of-range should not be fatal."],"exampleFix":"// before\nint v = list.Get(i);   // i may be == Size()\n// after\nif (i < 0 || i >= list.Size()) return;\nint v = list.Get(i);","handlingStrategy":"validation","validationCode":"// Before calling Get on my_list\nif (index >= 0 && index < list.Size()) {\n    int v = list.Get(index);\n}","typeGuard":null,"tryCatchPattern":"try { int v = list.Get(index); }\ncatch (IndexOutOfRangeException) { /* index out of range: clamp or report */ }","preventionTips":["Validate 0 <= index < Size() before Get.","Use '< Size()' not '<= Size()' in loop bounds.","Do not confuse Size() with Capacity().","Prefer the list's Size() over the backing array length for bounds."],"tags":["csharp","list","data-structure","bounds","index","precondition"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}