Join Nostr
2026-10-26 17:08:02 UTC

codementor on Nostr: Learn: Which end of a Stack<T> is the top? new Stack<int>(items) pushes the items in ...

Learn: Which end of a Stack<T> is the top?

new Stack<int>(items) pushes the items in order, so the last item ends up on top.

foreach, ToArray and string.Join all go from the top down, not in the order you added the items.

```csharp
var s = new Stack<int>(new[] { 1, 2, 3 });
Console.WriteLine(s.Peek());
Console.WriteLine(string.Join(" ", s));
```

It prints 3, then 3 2 1.

This catches people when they print a stack to check their answer, so say "read from the top".

#sq005 · SQ-005-N