Skip to main content
Journey Uncommon Logo
JourneyUncommon
hardC#finalization queue and GC.CollectSingle-choice MCQ

What does the following code most likely print, and why? (single-threaded console app, no other allocations interleaved)

class Zombie { public static Zombie? Saved; ~Zombie() { Saved = this; } } var z = new Zombie(); var wr = new WeakReference(z); z = null; GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); Console.WriteLine(wr.Target != null);