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
Published at
2026-10-26 17:08:02 UTCEvent JSON
{
"id": "838858cd21d3a8de151b3acb887e10211aa5afeb7f96bddfd4e9f0c3bd08e563",
"pubkey": "e68a0856e90d9997b1f721e905249896412dd570399a987b2e8fd5e7fcfe2279",
"created_at": 1793034482,
"kind": 1,
"tags": [
[
"t",
"sq005"
]
],
"content": "Learn: Which end of a Stack\u003cT\u003e is the top?\n\nnew Stack\u003cint\u003e(items) pushes the items in order, so the last item ends up on top.\n\nforeach, ToArray and string.Join all go from the top down, not in the order you added the items.\n\n```csharp\nvar s = new Stack\u003cint\u003e(new[] { 1, 2, 3 });\nConsole.WriteLine(s.Peek());\nConsole.WriteLine(string.Join(\" \", s));\n```\n\nIt prints 3, then 3 2 1.\n\nThis catches people when they print a stack to check their answer, so say \"read from the top\".\n\n#sq005 · SQ-005-N",
"sig": "dca4f42ba87a623ef6cd735e476b0dad5ad46cf99e9d6ac6e3adf0e5a050adeb65a59f7e2f4d65f062e3cbb7795b7351a27035bc909054a8c248da003c76791d"
}