相关疑难解决方法(0)

了解.NET中的垃圾收集

考虑以下代码:

public class Class1
{
    public static int c;
    ~Class1()
    {
        c++;
    }
}

public class Class2
{
    public static void Main()
    {
        {
            var c1=new Class1();
            //c1=null; // If this line is not commented out, at the Console.WriteLine call, it prints 1.
        }
        GC.Collect();
        GC.WaitForPendingFinalizers();
        Console.WriteLine(Class1.c); // prints 0
        Console.Read();
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,即使main方法中的变量c1超出范围并且在GC.Collect()调用时没有被任何其他对象进一步引用,为什么它没有在那里完成?

.net c# garbage-collection

161
推荐指数
2
解决办法
5万
查看次数

标签 统计

.net ×1

c# ×1

garbage-collection ×1