{"record":{"id":"a585402207a0fe12","repo":"TheAlgorithms/C-Sharp","slug":"the-stack-contains-no-items","errorCode":null,"errorMessage":"The stack contains no items.","messagePattern":"The stack contains no items\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"DataStructures/Stack/QueueBasedStack.cs","lineNumber":31,"sourceCode":"\n    public bool IsEmpty() => queue.Count == 0;\n\n    /// <summary>\n    ///     Adds an item on top of the stack.\n    /// </summary>\n    /// <param name=\"item\">Item to be added on top of stack.</param>\n    public void Push(T item) => queue.Enqueue(item);\n\n    /// <summary>\n    ///     Removes an item from  top of the stack and returns it.\n    ///  </summary>\n    /// <returns>item on top of stack.</returns>\n    /// <exception cref=\"InvalidOperationException\">Throw if stack is empty.</exception>\n    public T Pop()\n    {\n        if (IsEmpty())\n        {\n            throw new InvalidOperationException(\"The stack contains no items.\");\n        }\n\n        for (int i = 0; i < queue.Count - 1; i++)\n        {\n            queue.Enqueue(queue.Dequeue());\n        }\n\n        return queue.Dequeue();\n    }\n\n    /// <summary>\n    ///     return an item from the top of the stack without removing it.\n    /// </summary>\n    /// <returns>item on top of the stack.</returns>\n    /// <exception cref=\"InvalidOperationException\">Throw if stack is empty.</exception>\n    public T Peek()\n    {\n        if (IsEmpty())","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/Stack/QueueBasedStack.cs#L13-L49","documentation":"QueueBasedStack.Pop throws InvalidOperationException when the stack is empty, since there is no top item to remove and return. The guard `if (IsEmpty())` at the top of Pop ensures the caller never dequeues from the backing queue without at least one element present.","triggerScenarios":"Calling Pop() on a new QueueBasedStack<T>, or after popping/peeking has consumed all pushed items (Push(a); Pop(); Pop() on a one-element stack).","commonSituations":"Looping over a collection pushing items but miscounting iterations so one extra Pop happens; popping from a stack shared across methods where one consumer already drained it; forgetting to check Count/IsEmpty before pop in generic algorithms.","solutions":["Check IsEmpty() (or Count > 0) before calling Pop","Wrap Pop in try/catch for InvalidOperationException when empty-pop is an expected condition","Refactor to use TryPop-style logic: if (stack.IsEmpty()) return default/early-exit instead of popping"],"exampleFix":"// before\nvar item = stack.Pop();\n// after\nif (stack.IsEmpty())\n{\n    throw new InvalidOperationException(\"Cannot pop from an empty stack.\"); // or return default\n}\nvar item = stack.Pop();","handlingStrategy":"validation","validationCode":"if (stack == null) throw new ArgumentNullException(nameof(stack));\nif (stack.IsEmpty()) { /* handle empty: return default / skip / throw custom */ }","typeGuard":null,"tryCatchPattern":"try { var item = stack.Pop(); }\ncatch (InvalidOperationException) { /* stack was empty — take fallback path */ }","preventionTips":["Always call IsEmpty() before Pop in generic algorithms","Track pushed/popped counts in loops to avoid one extra pop","Prefer an explicit TryPop-style wrapper for optional pops"],"tags":["c-sharp","data-structures","stack","empty-collection"],"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"}