Tag: 终结器

GC.Collect()不立即收集?

在聊天的讨论过程中,我编写了这个控制台应用程序。 码: 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打印之前,我原本预计应用程序会崩溃。 […]