{"record":{"id":"a379c0a832e72d81","repo":"TheAlgorithms/C-Sharp","slug":"there-are-no-items-in-the-queue","errorCode":null,"errorMessage":"There are no items in the queue.","messagePattern":"There are no items in the queue\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"DataStructures/Queue/ArrayBasedQueue.cs","lineNumber":43,"sourceCode":"    ///     Clears the queue.\n    /// </summary>\n    public void Clear()\n    {\n        startIndex = 0;\n        endIndex = 0;\n        isEmpty = true;\n        isFull = false;\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 (IsEmpty())\n        {\n            throw new InvalidOperationException(\"There are no items in the queue.\");\n        }\n\n        var dequeueIndex = endIndex;\n        endIndex++;\n        if (endIndex >= queue.Length)\n        {\n            endIndex = 0;\n        }\n\n        isFull = false;\n        isEmpty = startIndex == endIndex;\n\n        return queue[dequeueIndex];\n    }\n\n    /// <summary>\n    ///     Returns a boolean indicating whether the queue is empty.\n    /// </summary>","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/Queue/ArrayBasedQueue.cs#L25-L61","documentation":"ArrayBasedQueue.Dequeue throws InvalidOperationException('There are no items in the queue.') when IsEmpty() is true. Dequeuing from an empty queue is an invalid state operation; the error is thrown before any index math runs.","triggerScenarios":"Calling Dequeue() on a fresh queue or after all items were dequeued, e.g. consumer loops that dequeue more times than producers enqueued.","commonSituations":"BFS/producer-consumer code where the queue drains faster than expected, or missing the enqueue step on an early-exit path.","solutions":["Check IsEmpty() (or Count) before each Dequeue.","Catch InvalidOperationException around Dequeue when drain-to-empty is normal.","Fix producer logic so enqueue happens before consumer dequeue attempts."],"exampleFix":"// before\nvar item = queue.Dequeue(); // throws when empty\n// after\nif (!queue.IsEmpty())\n    var item = queue.Dequeue();","handlingStrategy":"validation","validationCode":"if (!queue.IsEmpty())\n    var item = queue.Dequeue();","typeGuard":null,"tryCatchPattern":"try { var item = queue.Dequeue(); }\ncatch (InvalidOperationException) { /* queue empty */ }","preventionTips":["Check IsEmpty() in consumer loops","Ensure producers enqueue before consumers dequeue","Bound dequeue loops by item count enqueued"],"tags":["empty-collection","queue","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"}