Skip to content
Skilled.dev

Free lesson walkthrough · C#

Queues in C#

Practice FIFO queue behavior in C#. Compare efficient front removal with a head cursor, preserve repeated values, and count fill-and-drain costs.

4 min read · Worked code, edge cases, and complexity

A queue processes items in first-in, first-out order. The item that has waited longest leaves next. The recording introduces that ordering through examples and operations. It helps to separate the queue's behavior from its storage representation: a linked list, circular buffer, or another suitable structure can implement the same ordering.

Trace the front and the back

Enqueue A, then B, then C. The front is A and the back is C. A dequeue returns A and moves the front to B. Enqueuing D afterward should leave the remaining order B, C, D. A structure that returns D next has implemented stack ordering instead.

Peeking reads the next item without removing it. Empty behavior needs a defined contract, especially when the queue permits values such as null or undefined. A size or explicit emptiness check can distinguish an absent item from an allowed stored value. Do not assume every language's queue API chooses the same return or error behavior.

Choose a representation with efficient endpoints

A linked queue that retains both endpoints can append at the back and remove at the front with constant link changes. A dynamic array that shifts every remaining value after each front removal can turn a long processing sequence into quadratic work. A moving head index avoids that repeated shift but may retain consumed storage until compaction or reuse.

A circular buffer reuses capacity by wrapping indexes. Its implementation must distinguish a full buffer from an empty one and decide whether it grows, rejects, or overwrites when full. Those policies belong to the implementation contract; FIFO ordering alone does not choose them.

Connect FIFO order to graph distance

Breadth-first search uses a queue so vertices are expanded by increasing distance from the start in an unweighted graph. Newly discovered neighbors wait behind previously discovered vertices at the same or earlier depth. Replacing the queue with a stack changes that guarantee. A visited set remains necessary when the graph can cycle or offer several routes to the same vertex.

Continue with Implement a Queue to follow endpoint changes. Then study Breadth-First Search and Pilot a Rocket, where FIFO processing supports a shortest route measured in number of edges. Weighted shortest paths require additional reasoning and a different ordering policy.

Preserve first-in, first-out order

The example enqueues every input value and then drains the queue. For [6, 2, 6], the output is [6, 2, 6]. The repeated six is a separate queue entry each time; equal payloads do not collapse into one item. An empty input produces an empty result.

The implementation varies with the host language. A deque or linked queue offers a direct front-removal operation. An array-backed example can instead advance a head cursor, avoiding a full shift on every removal. Advancing that cursor does not itself release earlier array entries, so an indefinitely growing service queue would need a compaction or ring-buffer policy.

Include the cost of draining the queue

With constant-time queue operations, filling and draining n entries takes O(n) time. The queue and returned list together use O(n) storage. Repeated front removal from a contiguous array may require moving the remaining suffix and turn draining into O(n²) work.

Test one item, repeated values, negative values, and the empty stream. The complete queue implementation lesson adds the more revealing lifecycle check: remove the last item, enqueue again, and verify that both endpoint references describe the new queue. Queue order also matters in breadth-first search, where a stack would produce a different traversal and would not establish unweighted shortest-path layers.

C# example and implementation choices

Queue<int> copies the input sequence into its own queue storage. Dequeue removes the oldest value, and Count protects the empty case before each call. A valid zero is handled exactly like any other integer. The method returns a separate List<int>; it does not expose the queue or mutate the supplied list. Queue capacity management belongs to the library implementation.

Worked example

using System;
using System.Collections.Generic;

public static class FifoOrderExample {
public static List<int> FifoOrder(List<int> values) {
    var queue = new Queue<int>(values);
    var output = new List<int>();
    while (queue.Count > 0) output.Add(queue.Dequeue());
    return output;
}
}

Continue in C#

Open Queues for the complete course explanation. The course keeps your saved programming-language choice. Use this standalone page to inspect the C# example and run it in your own local environment. Follow the original course sequence when you want the surrounding lesson context and related exercises.

The C# interview preparation guide connects these language choices with further practice and the cross-language operation reference.

Continue with the lesson

Open the original lesson to explore its full explanation and examples. Your saved programming-language choice stays unchanged. This reading page belongs to that lesson and does not create a separate progress record.