TheAlgorithms/C-Sharp · error · InvalidOperationException
Stack is empty
Error message
Stack is empty
What it means
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.
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.
Example fix
// before
var top = stack.Peek();
// after
if (stack.Count > 0)
{
var top = stack.Peek();
} Defensive patterns
Strategy: validation
Validate before calling
if (stack.Count == 0) return default; // or handle empty case
Type guard
static bool TryPeek<T>(ArrayBasedStack<T> stack, out T value) { if (stack.Count > 0) { value = stack.Peek(); return true; } value = default; return false; } Try / catch
try { var top = stack.Peek(); } catch (InvalidOperationException) { top = default; } Prevention
- Check Count before Peek/Pop in parsing loops.
- Track emptiness in loop conditions (while stack.Count > 0).
- Prefer Try-pattern wrappers around stack operations.
When it happens
Trigger: Calling Peek on a new/empty ArrayBasedStack, or after popping all pushed elements.
Common situations: 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.
Related errors
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/0b0dd0f01571369f.
Report an issue: GitHub.
Appendix: source
Thrown at DataStructures/Stack/ArrayBasedStack.cs:86
Capacity = DefaultCapacity;
}
/// <summary>
/// Determines whether an element is in the <see cref="ArrayBasedStack{T}" />.
/// </summary>
/// <param name="item">The item to locate in the <see cref="ArrayBasedStack{T}" />.</param>
/// <returns>True, if the item is in the stack.</returns>
public bool Contains(T item) => Array.IndexOf(stack, item, 0, top + 1) > -1;
/// <summary>
/// Returns the item at the top of the <see cref="ArrayBasedStack{T}" /> without removing it.
/// </summary>
/// <returns>The item at the top of the <see cref="ArrayBasedStack{T}" />.</returns>
public T Peek()
{
if (top == -1)
{
throw new InvalidOperationException(StackEmptyErrorMessage);
}
return stack[top];
}
/// <summary>
/// Removes and returns the item at the top of the <see cref="ArrayBasedStack{T}" />.
/// </summary>
/// <returns>The item removed from the top of the <see cref="ArrayBasedStack{T}" />.</returns>
public T Pop()
{
if (top == -1)
{
throw new InvalidOperationException(StackEmptyErrorMessage);
}
return stack[top--];
}View on GitHub (pinned to 96e2905cab)