TheAlgorithms/C-Sharp · error · InvalidOperationException

The queue has reached its capacity.

Error message

The queue has reached its capacity.

What it means

ArrayBasedQueue<T>.Enqueue throws InvalidOperationException when the fixed-capacity backing array is full. This library uses a statically-sized array, so the caller must size it correctly up front; the queue does not grow automatically.

Solutions

  1. Increase the queue's capacity at construction to at least the maximum number of items that can be enqueued (e.g. vertex count for BFS).
  2. Check IsFull() (or Count) before Enqueue and grow/flush the queue when needed.
  3. Switch to ListBasedQueue or StackBasedQueue (or System.Collections.Generic.Queue<T>), which grow dynamically.
  4. If capacity is intentionally fixed, wrap Enqueue in try-catch on InvalidOperationException and treat it as backpressure.

Example fix

// before
var queue = new ArrayBasedQueue<int>(10);
foreach (var v in graph.Vertices) queue.Enqueue(v); // throws when > 10
// after
var queue = new ArrayBasedQueue<int>(graph.Vertices.Count);
foreach (var v in graph.Vertices) queue.Enqueue(v);
Defensive patterns

Strategy: validation

Validate before calling

if (queue.IsFull())
{
    // grow, flush, or apply backpressure before enqueueing
}
else
{
    queue.Enqueue(item);
}

Try / catch

try
{
    queue.Enqueue(item);
}
catch (InvalidOperationException)
{
    // queue at capacity: resize or drop/backpressure
}

Prevention

When it happens

Trigger: Calling Enqueue when IsFull() is true — i.e. count == queue.Length. Typical with BFS-style callers (Bfs, BfsColor, LevelOrderTraversal, DeepestNode, InitializeQueueWithZeroInDegreeVertices, ProcessNeighbors) on graphs/trees larger than the queue's declared capacity.

Common situations: Constructing the queue with a capacity guessed smaller than the actual number of vertices/nodes to process (e.g. allocating n but traversing n+1 nodes), reusing an undersized queue across runs, or off-by-one capacity sizing in graph traversal code.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13). Data as JSON: /api/errors/49ba4314b9cfa726. Report an issue: GitHub.

Appendix: source

Thrown at DataStructures/Queue/ArrayBasedQueue.cs:91

    public T Peek()
    {
        if (IsEmpty())
        {
            throw new InvalidOperationException("There are no items in the queue.");
        }

        return queue[endIndex];
    }

    /// <summary>
    ///     Adds an item at the last position in the queue.
    /// </summary>
    /// <exception cref="InvalidOperationException">Thrown if the queue is full.</exception>
    public void Enqueue(T item)
    {
        if (IsFull())
        {
            throw new InvalidOperationException("The queue has reached its capacity.");
        }

        queue[startIndex] = item;

        startIndex++;
        if (startIndex >= queue.Length)
        {
            startIndex = 0;
        }

        isEmpty = false;
        isFull = startIndex == endIndex;
    }
}

View on GitHub (pinned to 96e2905cab)