{"record":{"id":"e423979d07a39b11","repo":"TheAlgorithms/C-Sharp","slug":"the-queue-contains-no-items","errorCode":null,"errorMessage":"The queue contains no items.","messagePattern":"The queue contains no items\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"DataStructures/Queue/StackBasedQueue.cs","lineNumber":41,"sourceCode":"\n    /// <summary>\n    ///     Clears the queue.\n    /// </summary>\n    public void Clear()\n    {\n        input.Clear();\n        output.Clear();\n    }\n\n    /// <summary>\n    ///     Returns the first item in the queue and removes it from the queue.\n    /// </summary>\n    /// <exception cref=\"InvalidOperationException\">Thrown if the queue is empty.</exception>\n    public T Dequeue()\n    {\n        if (input.Count == 0 && output.Count == 0)\n        {\n            throw new InvalidOperationException(\"The queue contains no items.\");\n        }\n\n        if (output.Count == 0)\n        {\n            while (input.Count > 0)\n            {\n                var item = input.Pop();\n                output.Push(item);\n            }\n        }\n\n        return output.Pop();\n    }\n\n    /// <summary>\n    ///     Returns a boolean indicating whether the queue is empty.\n    /// </summary>\n    public bool IsEmpty() => input.Count == 0 && output.Count == 0;","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/Queue/StackBasedQueue.cs#L23-L59","documentation":"StackBasedQueue<T>.Dequeue throws InvalidOperationException when both internal input and output stacks are empty, meaning no elements have been enqueued (or all have been dequeued). The two-stack implementation defers transfer, so only the combined emptiness matters.","triggerScenarios":"Calling Dequeue when input.Count == 0 && output.Count == 0 — on a fresh queue or a fully drained one.","commonSituations":"Consumer loops that dequeue a fixed number of times regardless of enqueue count, drained queue reuse, or a producer that enqueued nothing before consumption.","solutions":["Check Count > 0 before calling Dequeue.","Use a while (queue.Count > 0) drain loop instead of a for loop with a fixed count.","Catch InvalidOperationException when empty dequeue is an expected condition.","Verify enqueue logic runs before dequeue in producer/consumer flows."],"exampleFix":"// before\nwhile (hasWork) { var item = queue.Dequeue(); } // throws when drained\n// after\nwhile (queue.Count > 0) { var item = queue.Dequeue(); }","handlingStrategy":"validation","validationCode":"if (queue.Count == 0) return;\nvar item = queue.Dequeue();","typeGuard":null,"tryCatchPattern":"try\n{\n    var item = queue.Dequeue();\n}\ncatch (InvalidOperationException)\n{\n    // both stacks empty — nothing to dequeue\n}","preventionTips":["Guard with Count > 0 before Dequeue.","Use while (queue.Count > 0) for drain loops.","Ensure enqueues precede dequeues in producer/consumer flows.","Add unit tests covering dequeue-from-empty behavior."],"tags":["data-structures","queue","empty-collection","csharp"],"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"}