Hi,
Consider the following program:
class Program
{
class Sample
{
~Sample()
{
System.Diagnostics.Trace.WriteLine("Destructor is called..");
}
}
static void Main(string[] args)
{
System.Diagnostics.Trace.WriteLine("Experimenting destructors.");
Sample sample = new Sample();
sample = null;
System.Diagnostics.Trace.WriteLine("GC is about to start.");
GC.Collect();
GC.WaitForPendingFinalizers();
System.Diagnostics.Trace.WriteLine("GC is completed.");
Console.ReadKey();
}
}
In .NET Framework 4.7.2, I can see the destructor message clearly[In fact, I do not need to call the GC there]. But the same program in .NET 5(or .NET 6 or .NET Core 3.1), does not show the destructor message.
Additional info:
I have checked that .NET 5/6 shows the destructor message you compose the object into another object and the composed object is not inside the scope of Main().
I'd appreciate your thoughts on this.
Regards,
Vaskaran