Last active
April 25, 2023 19:57
-
-
Save jskeet/b80c0824506f4620190e958ae519222d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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