{"record":{"id":"8fdb31c171fafe07","repo":"TheAlgorithms/C-Sharp","slug":"index","errorCode":null,"errorMessage":"index","messagePattern":"index","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"DataStructures/LinkedList/SinglyLinkedList/SinglyLinkedList.cs","lineNumber":68,"sourceCode":"        {\n            tempElement = tempElement.Next;\n        }\n\n        // adds the new element to the last one\n        tempElement.Next = newListElement;\n        return newListElement;\n    }\n\n    /// <summary>\n    ///     Returns element at index <paramref name=\"index\" /> in the list.\n    /// </summary>\n    /// <param name=\"index\">Index of an element to be returned.</param>\n    /// <returns>Element at index <paramref name=\"index\" />.</returns>\n    public T GetElementByIndex(int index)\n    {\n        if (index < 0)\n        {\n            throw new ArgumentOutOfRangeException(nameof(index));\n        }\n\n        var tempElement = Head;\n\n        for (var i = 0; tempElement is not null && i < index; i++)\n        {\n            tempElement = tempElement.Next;\n        }\n\n        if (tempElement is null)\n        {\n            throw new ArgumentOutOfRangeException(nameof(index));\n        }\n\n        return tempElement.Data;\n    }\n\n    public int Length()","sourceCodeStart":50,"sourceCodeEnd":86,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/LinkedList/SinglyLinkedList/SinglyLinkedList.cs#L50-L86","documentation":"GetElementByIndex throws ArgumentOutOfRangeException with paramName 'index' when a negative index is requested. Negative indices are not supported (no from-the-end semantics) so the call is rejected before traversal.","triggerScenarios":"Calling GetElementByIndex(-1) or any negative value, typically from uninitialized counter variables or 1-based input not converted.","commonSituations":"Loop variables that underflow (i starts at 0 and is decremented), user-supplied positions parsed without sign validation.","solutions":["Validate index >= 0 before the call.","Fix loop/counter logic that can produce negative values.","Catch ArgumentOutOfRangeException around the call when input is untrusted."],"exampleFix":"// before\nvar item = list.GetElementByIndex(userIndex); // could be -1\n// after\nif (userIndex >= 0 && userIndex < list.Length())\n    var item = list.GetElementByIndex(userIndex);","handlingStrategy":"validation","validationCode":"if (index >= 0 && index < list.Length())\n    var item = list.GetElementByIndex(index);","typeGuard":"bool IsValidIndex(int i, int length) => i >= 0 && i < length;","tryCatchPattern":"try { var item = list.GetElementByIndex(index); }\ncatch (ArgumentOutOfRangeException) { /* invalid index */ }","preventionTips":["Reject negative user input before use","Fix counters that can underflow","Convert 1-based input with input - 1 and re-validate"],"tags":["argument-out-of-range","linked-list","csharp"],"backgroundTag":"index-out-of-range","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}