{"record":{"id":"1663636098807b32","repo":"TheAlgorithms/C-Sharp","slug":"there-are-no-items-in-the-queue-listbasedqueue","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/ListBasedQueue.cs","lineNumber":32,"sourceCode":"    public ListBasedQueue() => queue = new LinkedList<T>();\n\n    /// <summary>\n    ///     Clears the queue.\n    /// </summary>\n    public void Clear()\n    {\n        queue.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 (queue.First is null)\n        {\n            throw new InvalidOperationException(\"There are no items in the queue.\");\n        }\n\n        var item = queue.First;\n        queue.RemoveFirst();\n        return item.Value;\n    }\n\n    /// <summary>\n    ///     Returns a boolean indicating whether the queue is empty.\n    /// </summary>\n    public bool IsEmpty() => !queue.Any();\n\n    /// <summary>\n    ///     Returns a boolean indicating whether the queue is full.\n    /// </summary>\n    public bool IsFull() => false;\n\n    /// <summary>","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/Queue/ListBasedQueue.cs#L14-L50","documentation":"ListBasedQueue<T>.Dequeue throws InvalidOperationException when the underlying LinkedList has no first node, i.e. the queue is empty. The library chooses to fail fast rather than return default(T), so callers must ensure the queue is non-empty or handle the exception.","triggerScenarios":"Calling Dequeue on a newly constructed ListBasedQueue, or after dequeuing all enqueued items (queue.First is null).","commonSituations":"Consumer loops that call Dequeue more times than items were enqueued, producer/consumer code where the producer failed silently, or reusing a drained queue without checking Count.","solutions":["Check Count > 0 before calling Dequeue.","Restructure loops to use Count or an enumerator instead of a fixed number of Dequeue calls.","Catch InvalidOperationException around Dequeue if empty-consume is an expected case.","Fix producer logic so items are enqueued before consumption starts."],"exampleFix":"// before\nvar item = queue.Dequeue(); // throws if empty\n// after\nif (queue.Count > 0)\n{\n    var item = queue.Dequeue();\n}","handlingStrategy":"validation","validationCode":"if (queue.Count == 0)\n{\n    return default; // or handle empty case\n}\nvar item = queue.Dequeue();","typeGuard":null,"tryCatchPattern":"try\n{\n    var item = queue.Dequeue();\n}\ncatch (InvalidOperationException)\n{\n    // queue was empty\n}","preventionTips":["Check Count > 0 before Dequeue.","Use while (queue.Count > 0) drain loops, not fixed-count loops.","Verify producer enqueue completes before consumption.","Wrap Dequeue in a TryDequeue-style helper."],"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"}