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
Enqueue/Dequeuerun jobs in arrival order;Peeklooks ahead.Push/Popunwind history newest-first — the undo shape.- Both expose
Countand cost O(1) per operation.
Keywords and builtins used here
newstringvar
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.
Step 1 of 3 in More collections, step 10 of 17 in The .NET library.