{"record":{"id":"187ce1f01319903d","repo":"TheAlgorithms/C-Sharp","slug":"stack-is-empty-listbasedstack","errorCode":null,"errorMessage":"Stack is empty","messagePattern":"Stack is empty","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"DataStructures/Stack/ListBasedStack.cs","lineNumber":64,"sourceCode":"    /// </summary>\n    public void Clear() => stack.Clear();\n\n    /// <summary>\n    ///     Determines whether an element is in the <see cref=\"ListBasedStack{T}\" />.\n    /// </summary>\n    /// <param name=\"item\">The item to locate in the <see cref=\"ListBasedStack{T}\" />.</param>\n    /// <returns>True, if the item is in the stack.</returns>\n    public bool Contains(T item) => stack.Contains(item);\n\n    /// <summary>\n    ///     Returns the item at the top of the <see cref=\"ListBasedStack{T}\" /> without removing it.\n    /// </summary>\n    /// <returns>The item at the top of the <see cref=\"ListBasedStack{T}\" />.</returns>\n    public T Peek()\n    {\n        if (stack.First is null)\n        {\n            throw new InvalidOperationException(\"Stack is empty\");\n        }\n\n        return stack.First.Value;\n    }\n\n    /// <summary>\n    ///     Removes and returns the item at the top of the <see cref=\"ListBasedStack{T}\" />.\n    /// </summary>\n    /// <returns>The item removed from the top of the <see cref=\"ListBasedStack{T}\" />.</returns>\n    public T Pop()\n    {\n        if (stack.First is null)\n        {\n            throw new InvalidOperationException(\"Stack is empty\");\n        }\n\n        var item = stack.First.Value;\n        stack.RemoveFirst();","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/Stack/ListBasedStack.cs#L46-L82","documentation":"ListBasedStack<T>.Peek (backed by LinkedList<T>) returns the first element as the stack top. When the underlying list is empty (First is null) it throws InvalidOperationException with 'Stack is empty'.","triggerScenarios":"Calling Peek on an empty ListBasedStack — before any Push, or after all items were popped.","commonSituations":"Test code checking Peek on a fresh stack, iterative algorithms peeking between phases when the stack may have been fully consumed.","solutions":["Check stack.Count (or IsEmpty) before Peek.","Use try/catch around InvalidOperationException when empty is expected.","Restructure the loop so Peek is only called after at least one Push."],"exampleFix":"// before\nvar top = listStack.Peek();\n// after\nvar top = listStack.Count > 0 ? listStack.Peek() : default;","handlingStrategy":"validation","validationCode":"if (listStack.Count == 0) return default;","typeGuard":"static bool TryPeek<T>(ListBasedStack<T> stack, out T value) { if (stack.Count > 0) { value = stack.Peek(); return true; } value = default; return false; }","tryCatchPattern":"try { var top = listStack.Peek(); } catch (InvalidOperationException) { top = default; }","preventionTips":["Check Count/IsEmpty before Peek.","Only Peek after at least one successful Push.","Wrap Peek in Try-style helpers."],"tags":["stack","empty-collection","linked-list"],"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"}