相关疑难解决方法(0)

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

结果:

Collecting... Done

Unhandled Exception: System.Exception: Exception of type 'System.Exception' was
thrown.
   at SomeClass.Finalize()
Run Code Online (Sandbox Code Playgroud)

我本来以为应用程序 Done打印会崩溃.

我不太关心如何制作它.我的问题是,为什么不呢?

c# garbage-collection destructor finalizer

4
推荐指数
2
解决办法
4525
查看次数

标签 统计

c# ×1

destructor ×1

finalizer ×1

garbage-collection ×1