{"record":{"id":"49ba4314b9cfa726","repo":"TheAlgorithms/C-Sharp","slug":"the-queue-has-reached-its-capacity","errorCode":null,"errorMessage":"The queue has reached its capacity.","messagePattern":"The queue has reached its capacity\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"DataStructures/Queue/ArrayBasedQueue.cs","lineNumber":91,"sourceCode":"    public T Peek()\n    {\n        if (IsEmpty())\n        {\n            throw new InvalidOperationException(\"There are no items in the queue.\");\n        }\n\n        return queue[endIndex];\n    }\n\n    /// <summary>\n    ///     Adds an item at the last position in the queue.\n    /// </summary>\n    /// <exception cref=\"InvalidOperationException\">Thrown if the queue is full.</exception>\n    public void Enqueue(T item)\n    {\n        if (IsFull())\n        {\n            throw new InvalidOperationException(\"The queue has reached its capacity.\");\n        }\n\n        queue[startIndex] = item;\n\n        startIndex++;\n        if (startIndex >= queue.Length)\n        {\n            startIndex = 0;\n        }\n\n        isEmpty = false;\n        isFull = startIndex == endIndex;\n    }\n}\n","sourceCodeStart":73,"sourceCodeEnd":106,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/Queue/ArrayBasedQueue.cs#L73-L106","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["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).","Check IsFull() (or Count) before Enqueue and grow/flush the queue when needed.","Switch to ListBasedQueue or StackBasedQueue (or System.Collections.Generic.Queue<T>), which grow dynamically.","If capacity is intentionally fixed, wrap Enqueue in try-catch on InvalidOperationException and treat it as backpressure."],"exampleFix":"// before\nvar queue = new ArrayBasedQueue<int>(10);\nforeach (var v in graph.Vertices) queue.Enqueue(v); // throws when > 10\n// after\nvar queue = new ArrayBasedQueue<int>(graph.Vertices.Count);\nforeach (var v in graph.Vertices) queue.Enqueue(v);","handlingStrategy":"validation","validationCode":"if (queue.IsFull())\n{\n    // grow, flush, or apply backpressure before enqueueing\n}\nelse\n{\n    queue.Enqueue(item);\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    queue.Enqueue(item);\n}\ncatch (InvalidOperationException)\n{\n    // queue at capacity: resize or drop/backpressure\n}","preventionTips":["Size the queue to at least the maximum possible item count (e.g. graph vertex count).","Check IsFull() before Enqueue in loop-heavy traversal code.","Prefer dynamically-sized queues (ListBasedQueue) when capacity is unknown.","Assert capacity assumptions in tests with max-size inputs."],"tags":["data-structures","queue","capacity-exceeded","csharp"],"backgroundTag":"value-out-of-range","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"}