{"record":{"id":"dbce4b6a89479242","repo":"TheAlgorithms/C-Sharp","slug":"cannot-prune-empty-list","errorCode":null,"errorMessage":"Cannot prune empty list","messagePattern":"Cannot prune empty list","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"DataStructures/LinkedList/DoublyLinkedList/DoublyLinkedList.cs","lineNumber":248,"sourceCode":"        if (Head is null)\n        {\n            Tail = null;\n            Count = 0;\n            return;\n        }\n\n        Head.Previous = null;\n        Count--;\n    }\n\n    /// <summary>\n    ///     Removes the last node in the list.\n    /// </summary>\n    public void Remove()\n    {\n        if (Tail is null)\n        {\n            throw new InvalidOperationException(\"Cannot prune empty list\");\n        }\n\n        Tail = Tail.Previous;\n        if (Tail is null)\n        {\n            Head = null;\n            Count = 0;\n            return;\n        }\n\n        Tail.Next = null;\n        Count--;\n    }\n\n    /// <summary>\n    ///     Removes specific node.\n    /// </summary>\n    /// <param name=\"node\"> Node to be removed.</param>","sourceCodeStart":230,"sourceCodeEnd":266,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/LinkedList/DoublyLinkedList/DoublyLinkedList.cs#L230-L266","documentation":"This InvalidOperationException is the empty-list guard at the top of DoublyLinkedList.Remove (DoublyLinkedList.cs:248). Remove unlinks the tail node by moving Tail to Tail.Previous; when Tail is null the list is empty, so there is no last node to prune. It fires when Remove (or RemoveNode's delegated removal path) is invoked on a list with Count == 0.","triggerScenarios":"Calling Remove() on an empty list, or calling it more times than elements were added.","commonSituations":"Undo-style pop loops that pop once per event when some events added nothing, or miscounted insert/remove pairs.","solutions":["Check Count > 0 before calling Remove.","Catch InvalidOperationException if empty-at-pop-time is an expected condition.","Track add/remove balance so Remove is only called when a prior Add succeeded."],"exampleFix":"// before\nlist.Remove(); // may throw on empty\n// after\nif (list.Count > 0) list.Remove();","handlingStrategy":"validation","validationCode":"if (list.Count > 0) list.Remove();","typeGuard":null,"tryCatchPattern":"try { list.Remove(); }\ncatch (InvalidOperationException) { /* list was empty */ }","preventionTips":["Check Count before popping the tail","Balance Add/Remove calls explicitly","Wrap Remove in a SafeRemove helper returning bool"],"tags":["empty-collection","invalid-operation","csharp"],"backgroundTag":"empty-collection-operation","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"}