Skip to content

Instantly share code, notes, and snippets.

@jskeet
Last active April 25, 2023 19:57
Show Gist options
  • Save jskeet/b80c0824506f4620190e958ae519222d to your computer and use it in GitHub Desktop.
Save jskeet/b80c0824506f4620190e958ae519222d to your computer and use it in GitHub Desktop.
using System;
using System.Threading;
class Program
{
~Program()
{
Console.WriteLine("Finalizer executed.");
}
private void DemonstrateOddity()
{
Console.WriteLine("This is in an instance method.");
GC.Collect();
GC.WaitForPendingFinalizers();
// Just to prove it isn't a matter of the finalizer thread racing for console output
Thread.Sleep(5000);
Console.WriteLine("This is the end of the instance method.");
}
static void Main()
{
// Output in .NET 4.6.2 in release mode:
// This is in an instance method.
// Finalizer executed.
// This is the end of the instance method.
new Program().DemonstrateOddity();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment