.netに次のように書くことは合法ですか?
public class A
{
public int i = 0;
~A()
{
Aref = this;
}
}
public static A Aref;
static void Main(string[] args)
{
Aref = new A();
int gen = GC.GetGeneration(Aref);
Aref = null;
GC.Collect(gen, GCCollectionMode.Forced);
GC.WaitForPendingFinalizers();
Console.WriteLine(Aref.i);
Console.ReadLine();
}
期待どおりに動作し、コンソールに「0」を書き込みますが、常に動作することが保証されているのではないかと思います。
誰かが舞台裏で何が起こっているのか知っていますか?