{"record":{"id":"36563ae061fb33da","repo":"TheAlgorithms/C-Sharp","slug":"queue-is-empty","errorCode":null,"errorMessage":"Queue is empty.","messagePattern":"Queue is empty\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"Algorithms/Search/AStar/PriorityQueue.cs","lineNumber":128,"sourceCode":"        }\n\n        if (Count > 0)\n        {\n            list[i] = root;\n        }\n\n        return target;\n    }\n\n    /// <summary>\n    ///     Returns the next element in the queue without dequeuing.\n    /// </summary>\n    /// <returns>The next element of the queue.</returns>\n    public T Peek()\n    {\n        if (Count == 0)\n        {\n            throw new InvalidOperationException(\"Queue is empty.\");\n        }\n\n        return list[0];\n    }\n\n    /// <summary>\n    ///     Clears the Queue.\n    /// </summary>\n    public void Clear() => list.Clear();\n\n    /// <summary>\n    ///     Returns the Internal Data.\n    /// </summary>\n    /// <returns>The internal data structure.</returns>\n    public List<T> GetData() => list;\n}\n","sourceCodeStart":110,"sourceCodeEnd":145,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Search/AStar/PriorityQueue.cs#L110-L145","documentation":"PriorityQueue.Peek throws InvalidOperationException when the queue has no elements, since there is no front element to return. Called by GenerateShortestPath/target in AStar, it surfaces there when the code peeks a queue that should be non-empty.","triggerScenarios":"Peeking an empty queue directly, or reaching it via AStar when the search emptied the priority queue before the target was reconstructed (e.g. target unreachable and the path-generation step still calls Peek).","commonSituations":"Querying a path to an unreachable node in a disconnected graph; target node not present in the graph; caller forgetting Count/IsEmpty check before Peek.","solutions":["Check Count > 0 before calling Peek (or use TryPeek if available).","Verify the target node exists and is reachable before asking AStar for a path.","Handle unreachable-target cases in calling code instead of letting the queue throw."],"exampleFix":"// before\nvar node = queue.Peek();\n// after\nif (queue.Count == 0) return null;\nvar node = queue.Peek();","handlingStrategy":"try-catch","validationCode":"if (queue.Count == 0) return null; // or handle empty case before peeking","typeGuard":null,"tryCatchPattern":"try { var top = queue.Peek(); }\ncatch (InvalidOperationException) { return null; } // queue empty: target unreachable","preventionTips":["Check Count/IsEmpty before Peek or Dequeue","Confirm target reachability before path reconstruction","Treat empty-queue Peek as 'no path' in pathfinding callers"],"tags":["empty-collection","csharp","pathfinding"],"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"}