{"record":{"id":"0b0dd0f01571369f","repo":"TheAlgorithms/C-Sharp","slug":"stack-is-empty","errorCode":null,"errorMessage":"Stack is empty","messagePattern":"Stack is empty","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"DataStructures/Stack/ArrayBasedStack.cs","lineNumber":86,"sourceCode":"        Capacity = DefaultCapacity;\n    }\n\n    /// <summary>\n    ///     Determines whether an element is in the <see cref=\"ArrayBasedStack{T}\" />.\n    /// </summary>\n    /// <param name=\"item\">The item to locate in the <see cref=\"ArrayBasedStack{T}\" />.</param>\n    /// <returns>True, if the item is in the stack.</returns>\n    public bool Contains(T item) => Array.IndexOf(stack, item, 0, top + 1) > -1;\n\n    /// <summary>\n    ///     Returns the item at the top of the <see cref=\"ArrayBasedStack{T}\" /> without removing it.\n    /// </summary>\n    /// <returns>The item at the top of the <see cref=\"ArrayBasedStack{T}\" />.</returns>\n    public T Peek()\n    {\n        if (top == -1)\n        {\n            throw new InvalidOperationException(StackEmptyErrorMessage);\n        }\n\n        return stack[top];\n    }\n\n    /// <summary>\n    ///     Removes and returns the item at the top of the <see cref=\"ArrayBasedStack{T}\" />.\n    /// </summary>\n    /// <returns>The item removed from the top of the <see cref=\"ArrayBasedStack{T}\" />.</returns>\n    public T Pop()\n    {\n        if (top == -1)\n        {\n            throw new InvalidOperationException(StackEmptyErrorMessage);\n        }\n\n        return stack[top--];\n    }","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/Stack/ArrayBasedStack.cs#L68-L104","documentation":"This InvalidOperationException is the empty-stack guard in ArrayBasedStack<T>.Peek (ArrayBasedStack.cs:86), where the shared sentinel StackEmptyErrorMessage ('Stack is empty') is thrown. Peek inspects the internal top index, which is -1 when nothing has been pushed (or everything was popped), so there is no top element to return. It fires when Peek is called before any Push or after enough Pop calls to empty the stack.","triggerScenarios":"Calling Peek on a new/empty ArrayBasedStack, or after popping all pushed elements.","commonSituations":"Parsing loops (e.g. expression or parenthesis evaluation) that Peek operators/brackets without first checking Count == 0; drain-and-peek patterns after a Pop loop.","solutions":["Check stack.Count > 0 before calling Peek.","Wrap in try/catch for InvalidOperationException when emptiness is an expected condition.","Restructure to use TryPeek-style logic or track emptiness in the loop condition."],"exampleFix":"// before\nvar top = stack.Peek();\n// after\nif (stack.Count > 0)\n{\n    var top = stack.Peek();\n}","handlingStrategy":"validation","validationCode":"if (stack.Count == 0) return default; // or handle empty case","typeGuard":"static bool TryPeek<T>(ArrayBasedStack<T> stack, out T value) { if (stack.Count > 0) { value = stack.Peek(); return true; } value = default; return false; }","tryCatchPattern":"try { var top = stack.Peek(); } catch (InvalidOperationException) { top = default; }","preventionTips":["Check Count before Peek/Pop in parsing loops.","Track emptiness in loop conditions (while stack.Count > 0).","Prefer Try-pattern wrappers around stack operations."],"tags":["stack","empty-collection","invalid-state"],"backgroundTag":"pop-from-empty-collection","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"}