typestar

Queue & Stack in C#

FIFO and LIFO, named for what they do.

var jobs = new Queue<string>();
jobs.Enqueue("build");
jobs.Enqueue("test");
jobs.Enqueue("deploy");
Console.WriteLine($"next: {jobs.Peek()}, waiting: {jobs.Count}");
Console.WriteLine($"ran {jobs.Dequeue()}, then {jobs.Dequeue()}");

var undo = new Stack<string>();
undo.Push("typed a");
undo.Push("typed b");
Console.WriteLine($"undo {undo.Pop()}, then undo {undo.Pop()}");

How it works

  1. Enqueue/Dequeue run jobs in arrival order; Peek looks ahead.
  2. Push/Pop unwind history newest-first — the undo shape.
  3. Both expose Count and cost O(1) per operation.

Keywords and builtins used here

The run, in numbers

Lines
11
Characters to type
375
Tokens
78
Three-star pace
95 tpm

At the three-star pace of 95 tokens a minute, this run takes about 49 seconds.

Type this snippet

Step 1 of 3 in More collections, step 10 of 17 in The .NET library.

← Previous Next →