{"record":{"id":"63cc5b6a1ddd686e","repo":"TheAlgorithms/C-Sharp","slug":"deque-is-empty","errorCode":null,"errorMessage":"Deque is empty.","messagePattern":"Deque is empty\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"DataStructures/Deque/Deque.cs","lineNumber":139,"sourceCode":"        count++;\n    }\n\n    /// <summary>\n    ///     Removes and returns the element at the front of the deque.\n    ///     This operation is O(1) time complexity.\n    /// </summary>\n    /// <returns>The element at the front of the deque.</returns>\n    /// <exception cref=\"InvalidOperationException\">Thrown when the deque is empty.</exception>\n    /// <example>\n    ///     // Deque: [3, 5, 7].\n    ///     int value = deque.RemoveFront();  // Returns 3, Deque: [5, 7].\n    /// </example>\n    public T RemoveFront()\n    {\n        // Validate that deque is not empty\n        if (IsEmpty)\n        {\n            throw new InvalidOperationException(\"Deque is empty.\");\n        }\n\n        // Retrieve the front element\n        T item = items[front];\n\n        // Clear the reference to help garbage collection\n        items[front] = default!;\n\n        // Move front pointer forward in circular fashion\n        front = (front + 1) % items.Length;\n        count--;\n\n        return item;\n    }\n\n    /// <summary>\n    ///     Removes and returns the element at the rear of the deque.\n    ///     This operation is O(1) time complexity.","sourceCodeStart":121,"sourceCodeEnd":157,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/Deque/Deque.cs#L121-L157","documentation":"RemoveFront() throws InvalidOperationException when the deque has no elements. The library validates IsEmpty before accessing items[front] because a circular-array deque has no sentinel object to return when Count == 0. It is a deliberate fail-fast contract: removing from an empty deque is a caller bug, not a recoverable condition.","triggerScenarios":"Calling RemoveFront() when Count == 0, i.e. on a newly constructed Deque<T>, after removing all elements (front and rear drained to empty), or calling it a second time after a prior RemoveFront/RemoveRear emptied the deque.","commonSituations":"Looping 'while' over a deque draining elements without checking Count/IsEmpty; off-by-one bookkeeping where the consumer removes one more item than was added; shared deque consumed by multiple callers where another consumer emptied it first.","solutions":["Check the IsEmpty property (or Count == 0) before calling RemoveFront()","Wrap the call in try-catch for InvalidOperationException if emptiness is expected and recoverable","Use PeekFront() behind an IsEmpty check to inspect without removing","Fix producer/consumer balance so consumers never out-drain producers"],"exampleFix":"// before\nT item = deque.RemoveFront();\n// after\nif (deque.IsEmpty) return;\nT item = deque.RemoveFront();","handlingStrategy":"try-catch","validationCode":"if (deque == null) throw new ArgumentNullException(nameof(deque));\nif (deque.IsEmpty)\n    return; // or default(T) / Optional<T>.None\nT item = deque.RemoveFront();","typeGuard":"bool CanRemoveFront<T>(Deque<T> d) => d != null && !d.IsEmpty;","tryCatchPattern":"try\n{\n    T item = deque.RemoveFront();\n    Process(item);\n}\ncatch (InvalidOperationException ex) when (ex.Message == \"Deque is empty.\")\n{\n    // deque drained; handle empty case\n}","preventionTips":["Always check IsEmpty or Count > 0 before any Remove/Peek call","In drain loops use while (!deque.IsEmpty) { ... RemoveFront(); }","For shared deques, re-check emptiness immediately before removal (or lock)","Prefer PeekFront with an IsEmpty guard when the element must survive a failed removal"],"tags":["csharp","data-structures","deque","empty-collection"],"backgroundTag":"empty-result-set","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"}