I build .NET6 console project under release mode and when it's started from command line finalizer won't be called. While starting it from IDE calls the finalizer successfully.
.NET Framework 4.8 - calls finalizer from cmd and IDE
.NET Core 3.1 - didn't call finalizer both from IDE and cmd
Does it meen that some settings to the project should be made? Or I just can't rely on finalizer even if GC.Collect() is force called?
namespace TestApp
{
public class Program
{
public static void Main(string[] args)
{
MyClass myClass = new MyClass();
var gen = GC.GetGeneration(myClass);
Console.WriteLine(gen);
myClass = null;
GC.Collect();
GC.WaitForPendingFinalizers();
Console.ReadKey();
}
}
public class MyClass
{
public MyClass()
{
Console.WriteLine("Ctor");
}
~ MyClass()
{
Console.WriteLine("Finalizer");
}
}
}
Output when .exe started from command-line (both cmd and powershell):
Ctor
0
Output when project started from IDE (Visual Studio):
Ctor
0
Finalized