{"record":{"id":"ca9d78ababc51ed7","repo":"krahets/hello-algo","slug":"index-out-of-bounds-ca9d78","errorCode":null,"errorMessage":"Index out of bounds","messagePattern":"Index out of bounds","errorType":"exception","errorClass":"IndexOutOfRangeException","httpStatus":null,"severity":"error","filePath":"en/codes/csharp/chapter_array_and_linkedlist/my_list.cs","lineNumber":35,"sourceCode":"    public MyList() {\n        arr = new int[arrCapacity];\n    }\n\n    /* Get list length (current number of elements) */\n    public int Size() {\n        return arrSize;\n    }\n\n    /* Get list capacity */\n    public int Capacity() {\n        return arrCapacity;\n    }\n\n    /* Update element */\n    public int Get(int index) {\n        // If the index is out of bounds, throw an exception, as below\n        if (index < 0 || index >= arrSize)\n            throw new IndexOutOfRangeException(\"Index out of bounds\");\n        return arr[index];\n    }\n\n    /* Add elements at the end */\n    public void Set(int index, int num) {\n        if (index < 0 || index >= arrSize)\n            throw new IndexOutOfRangeException(\"Index out of bounds\");\n        arr[index] = num;\n    }\n\n    /* Direct traversal of list elements */\n    public void Add(int num) {\n        // When the number of elements exceeds capacity, trigger the extension mechanism\n        if (arrSize == arrCapacity)\n            ExtendCapacity();\n        arr[arrSize] = num;\n        // Update the number of elements\n        arrSize++;","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/en/codes/csharp/chapter_array_and_linkedlist/my_list.cs#L17-L53","documentation":"IndexOutOfRangeException with message \"Index out of bounds\" is thrown by MyList.Get(int index) when index is negative or >= arrSize (the current element count, not capacity). This is a manual bounds guard wrapping raw array access at arr[index].","triggerScenarios":"Calling Get(index) where index < 0 or index >= list.Size(). For a freshly constructed MyList, arrSize is 0, so Get(0) already throws.","commonSituations":"Iterating with an off-by-one loop bound (e.g., <= instead of <), reading an index computed from external input without clamping, or accessing a list before any Add() calls.","solutions":["Validate 0 <= index < list.Size() before calling Get().","Fix off-by-one loop bounds: use i < list.Size(), never i <= list.Size().","For external input, clamp the index to [0, list.Size()-1] or reject out-of-range values explicitly."],"exampleFix":"// before\nMyList list = new();\nlist.Add(10);\nint val = list.Get(1); // throws — Size() is 1, valid range [0,0]\n\n// after\nif (index >= 0 && index < list.Size()) {\n    int val = list.Get(index);\n}","handlingStrategy":"validation","validationCode":"if (index >= 0 && index < list.Size()) {\n    int val = list.Get(index);\n}","typeGuard":null,"tryCatchPattern":"try {\n    int val = list.Get(index);\n} catch (IndexOutOfRangeException) {\n    // index out of bounds — handle\n}","preventionTips":["Use i < list.Size() in loops, never i <= list.Size().","Validate externally-sourced indices before passing to Get().","Remember Size() returns element count, not array capacity."],"tags":["csharp","array","index-out-of-bounds","precondition","list"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}