在聊天讨论中,我写了这个控制台应用程序。
代码:
using System;
class Program
{
static void Main(string[] args)
{
CreateClass();
Console.Write("Collecting... ");
GC.Collect();
Console.WriteLine("Done");
}
static void CreateClass()
{
SomeClass c = new SomeClass();
}
}
class SomeClass
{
~SomeClass()
{
throw new Exception();
}
}
结果:
Collecting... Done
Unhandled Exception: System.Exception: Exception of type 'System.Exception' was
thrown.
at SomeClass.Finalize()
在打印Done
之前,我本以为这个应用程序会崩溃。
我不太关心如何制作它。我的问题是,为什么它没有崩溃呢?
GC.WaitForPendingFinalizers
的作用:http://msdn.microsoft.com/zh-cn/library/system.gc.waitforpendingfinalizers.aspx - Ry-