Read the syntax
PriorityQueue<TElement,TPriority> retrieves the element with the lowest priority value. It is available in .NET 6+ and does not guarantee FIFO order for ties. Use a .NET 8+ console project; replace Program.cs with this C# example.
The queue orders by numeric priority independently of the string payload. TryDequeue ends the loop without throwing on an empty queue.
Syntax
queue.Enqueue(element, priority); queue.Dequeue()Examples
PriorityQueue Scheduling
Use a .NET 8+ console project; replace Program.cs with this C# example.
using System;
using System.Collections.Generic;
var jobs = new PriorityQueue<string, int>();
jobs.Enqueue("cleanup", 3);
jobs.Enqueue("urgent", 1);
jobs.Enqueue("report", 2);
while (jobs.TryDequeue(out string? job, out int priority))
Console.WriteLine($"{priority}: {job}");- Expected output: 1: urgent 2: report 3: cleanup
- The queue orders by numeric priority independently of the string payload. TryDequeue ends the loop without throwing on an empty queue.
- Add a sequence number to priority when stable ordering of ties is required.
Best practices
- Add a sequence number to priority when stable ordering of ties is required.
- Do not assume UnorderedItems yields priority order.
Runtime & versions
.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
C# language reference