backendgym backendgym · reps Sign in with GitHubSign in

05Two handles, one dispose

csharp-reading / reps/05/README.md

Read Program.cs first, then run it: dotnet run --project reps/05. It prints:

open a
open b
dispose a
caught boom
finally
end

b is never disposed, not even after GC.Collect().

After the rep
  1. 1 Why does dispose a come before caught boom, and not after it?
  2. 2 Why didn't forcing a collection dispose b? If your answer uses the word "eventually", keep looking.
  3. 3 What's the smallest change that would have disposed b, and the smallest that makes the class safe even when somebody forgets?