{"record":{"id":"80ba79589089a985","repo":"TheAlgorithms/C-Sharp","slug":"heap-is-empty","errorCode":null,"errorMessage":"Heap is empty!","messagePattern":"Heap is empty!","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"DataStructures/Heap/BinaryHeap.cs","lineNumber":81,"sourceCode":"        data.Add(element);\n        HeapifyUp(data.Count - 1);\n    }\n\n    /// <summary>\n    ///     Remove the top/root of the binary heap (ie: the largest/smallest element).\n    /// </summary>\n    /// <remarks>\n    ///     Removing from the heap is done by swapping the top/root with the last element in\n    ///     the backing list, removing the last element, and pushing the new root down\n    ///     until the heap property is restored.\n    /// </remarks>\n    /// <returns>The top/root of the heap.</returns>\n    /// <exception cref=\"InvalidOperationException\">Thrown if heap is empty.</exception>\n    public T Pop()\n    {\n        if (Count == 0)\n        {\n            throw new InvalidOperationException(\"Heap is empty!\");\n        }\n\n        var elem = data[0];\n        data[0] = data[^1];\n        data.RemoveAt(data.Count - 1);\n        HeapifyDown(0);\n\n        return elem;\n    }\n\n    /// <summary>\n    ///     Return the top/root of the heap without removing it.\n    /// </summary>\n    /// <returns>The top/root of the heap.</returns>\n    /// <exception cref=\"InvalidOperationException\">Thrown if heap is empty.</exception>\n    public T Peek()\n    {\n        if (Count == 0)","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/Heap/BinaryHeap.cs#L63-L99","documentation":"BinaryHeap.Pop() throws InvalidOperationException when the heap has zero elements, because there is no root to return. It is a deliberate guard so callers never receive a default T from an empty collection.","triggerScenarios":"Calling Pop() on a new BinaryHeap<T> or after popping all previously pushed elements (Count == 0).","commonSituations":"Draining a priority queue in a loop without checking Count, popping before any Push, or a producer/consumer race where the queue was consumed by another path.","solutions":["Check Count > 0 (or TryPop-style pattern) before calling Pop().","Wrap Pop() in try-catch on InvalidOperationException if emptiness is expected.","Fix loop logic so Pop is only called once per Push."],"exampleFix":"// before\nvar top = heap.Pop();\n// after\nif (heap.Count > 0)\n{\n    var top = heap.Pop();\n}","handlingStrategy":"validation","validationCode":"if (heap.Count > 0) { var top = heap.Pop(); }","typeGuard":null,"tryCatchPattern":"try { var top = heap.Pop(); } catch (InvalidOperationException) { /* handle empty */ }","preventionTips":["Check Count before every Pop","Prefer a drain loop of while (heap.Count > 0)","Document that Pop is only valid on non-empty heaps"],"tags":["data-structures","heap","invalid-operation"],"backgroundTag":"unsupported-operation","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"}