{"record":{"id":"e4ee44f56145fc2a","repo":"TheAlgorithms/C-Sharp","slug":"max-count-is-count","errorCode":null,"errorMessage":"Max count is {Count}","messagePattern":"Max count is (.+?)","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"DataStructures/LinkedList/DoublyLinkedList/DoublyLinkedList.cs","lineNumber":207,"sourceCode":"            }\n\n            current = current.Next;\n        }\n\n        throw new ItemNotFoundException();\n    }\n\n    /// <summary>\n    ///     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        {","sourceCodeStart":189,"sourceCodeEnd":225,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/LinkedList/DoublyLinkedList/DoublyLinkedList.cs#L189-L225","documentation":"GetAt(position) throws ArgumentOutOfRangeException when the requested position is negative or >= Count. The message reports the maximum valid count so the caller can see how far off the request was. Note the message string is passed as the paramName-style first argument, so the text appears as the parameter name in the exception output.","triggerScenarios":"Calling GetAt with a negative integer, or calling GetAt(Count) or higher on a list with Count elements. Also calling GetAt on an empty list (Count == 0) with any position >= 0.","commonSituations":"Off-by-one loops (iterating i <= Count), using 1-based positions from user input against a 0-based API, or calling GetAt after removals emptied the list.","solutions":["Clamp or validate position against list.Count before calling: 0 <= position < Count.","Check Count (or IsEmpty) before indexing, especially after Remove/RemoveHead operations.","If the caller's input is 1-based, subtract 1 before calling GetAt."],"exampleFix":"// before\nvar node = list.GetAt(list.Count); // off by one\n// after\nif (list.Count > 0)\n    var node = list.GetAt(list.Count - 1);","handlingStrategy":"validation","validationCode":"if (position >= 0 && position < list.Count)\n    var node = list.GetAt(position);","typeGuard":"bool IsValidPosition(int p, int count) => p >= 0 && p < count;","tryCatchPattern":"try { var node = list.GetAt(position); }\ncatch (ArgumentOutOfRangeException) { /* handle missing index */ }","preventionTips":["Always bound-check against the live Count, not a cached value","Remember the API is 0-based","Recheck Count after any Add/Remove mutation"],"tags":["argument-out-of-range","linked-list","csharp"],"backgroundTag":"argument-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"}