{"record":{"id":"fd97852c9bb81efd","repo":"TheAlgorithms/C-Sharp","slug":"nameof-position-must-be-an-index-in-the-list","errorCode":null,"errorMessage":"{nameof(position)} must be an index in the list","messagePattern":"(.+?) must be an index in the list","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"DataStructures/LinkedList/DoublyLinkedList/DoublyLinkedList.cs","lineNumber":216,"sourceCode":"    ///     Looks for a node in the list that contains the value of the parameter.\n    /// </summary>\n    /// <param name=\"position\"> Position in the list.</param>\n    /// <returns>The node in the list the has the paramater as a value or null if not found.</returns>\n    /// <exception cref=\"ArgumentOutOfRangeException\">Thrown when position is negative or out range of the list.</exception>\n    public DoublyLinkedListNode<T> GetAt(int position)\n    {\n        if (position < 0 || position >= Count)\n        {\n            throw new ArgumentOutOfRangeException($\"Max count is {Count}\");\n        }\n\n        var current = Head;\n        for (var i = 0; i < position; i++)\n        {\n            current = current!.Next;\n        }\n\n        return current ?? throw new ArgumentOutOfRangeException($\"{nameof(position)} must be an index in the list\");\n    }\n\n    /// <summary>\n    ///     Removes the Head and replaces it with the second node in the list.\n    /// </summary>\n    public void RemoveHead()\n    {\n        if (Head is null)\n        {\n            throw new InvalidOperationException();\n        }\n\n        Head = Head.Next;\n        if (Head is null)\n        {\n            Tail = null;\n            Count = 0;\n            return;","sourceCodeStart":198,"sourceCodeEnd":234,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/LinkedList/DoublyLinkedList/DoublyLinkedList.cs#L198-L234","documentation":"After walking position nodes from Head, GetAt throws ArgumentOutOfRangeException named 'position' if the traversal lands on null, meaning position pointed past the last node. This is the tail-end guard for indices that passed the initial range check only because the list state changed or the walk ended on a null Next chain.","triggerScenarios":"Calling GetAt with a position that walks past the end of the chain, e.g. GetAt(Count) or on a list whose node links were corrupted/mutated concurrently so current becomes null before i reaches position.","commonSituations":"Race conditions where another thread removes nodes mid-traversal, or calling GetAt between list mutations that leave Count stale relative to the node chain.","solutions":["Validate position < list.Count immediately before the call and avoid sharing the list across threads without synchronization.","Catch ArgumentOutOfRangeException around GetAt and handle the missing-node case.","Re-fetch Count after any mutation rather than caching it."],"exampleFix":"// before\nvar node = list.GetAt(i); // i captured before list shrank\n// after\nif (i < list.Count)\n    var node = list.GetAt(i);\nelse\n    // handle missing index","handlingStrategy":"validation","validationCode":"if (position >= 0 && position < list.Count)\n    var node = list.GetAt(position);","typeGuard":null,"tryCatchPattern":"try { var node = list.GetAt(position); }\ncatch (ArgumentOutOfRangeException) { /* position no longer valid */ }","preventionTips":["Avoid concurrent mutation without locking","Refetch Count immediately before indexed access","Treat node references as invalid after list mutations"],"tags":["index-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"}