Понимание сборки мусора в .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();
}
}
Теперь, несмотря на то, что переменная c1 в методе main находится вне области видимости и на нее больше не ссылается ни один другой объект, когдаGC.Collect()
называется, почему это не завершено там?