TheAlgorithms/C-Sharp · error · InvalidOperationException
There are no items in the queue.
Error message
There are no items in the queue.
What it means
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.
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.
Example fix
// before
var item = queue.Dequeue(); // throws if empty
// after
if (queue.Count > 0)
{
var item = queue.Dequeue();
} Defensive patterns
Strategy: validation
Validate before calling
if (queue.Count == 0)
{
return default; // or handle empty case
}
var item = queue.Dequeue(); Try / catch
try
{
var item = queue.Dequeue();
}
catch (InvalidOperationException)
{
// queue was empty
} Prevention
- 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.
When it happens
Trigger: Calling Dequeue on a newly constructed ListBasedQueue, or after dequeuing all enqueued items (queue.First is null).
Common situations: 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.
Understand the failure class
Background: EmptyResultError / "no results found": when an API or scraper succeeds but returns zero rows — this error's family across 9 libraries.
Related errors
- The queue contains no items.
- Tree is empty!
- Deque is empty.
- There are no items in the queue.
- The queue has reached its capacity.
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/1663636098807b32.
Report an issue: GitHub.
Appendix: source
Thrown at DataStructures/Queue/ListBasedQueue.cs:32
public ListBasedQueue() => queue = new LinkedList<T>();
/// <summary>
/// Clears the queue.
/// </summary>
public void Clear()
{
queue.Clear();
}
/// <summary>
/// Returns the first item in the queue and removes it from the queue.
/// </summary>
/// <exception cref="InvalidOperationException">Thrown if the queue is empty.</exception>
public T Dequeue()
{
if (queue.First is null)
{
throw new InvalidOperationException("There are no items in the queue.");
}
var item = queue.First;
queue.RemoveFirst();
return item.Value;
}
/// <summary>
/// Returns a boolean indicating whether the queue is empty.
/// </summary>
public bool IsEmpty() => !queue.Any();
/// <summary>
/// Returns a boolean indicating whether the queue is full.
/// </summary>
public bool IsFull() => false;
/// <summary>View on GitHub (pinned to 96e2905cab)