扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
class A { ~A() { Console.WriteLine("Destruct instance of A"); } } class B { object ref; public B(object o) { ref = o; } ~B() { Console.WriteLine("Destruct instance of B"); } } class TMest { static void Main() { B b = new B(new A()); b = null; GC.Collect(); GC.WaitForPendingFinalizers(); } } |
Destruct instance of A Destruct instance of B 与 Destruct instance of B Destruct instance of A |
class A { ~A() { Console.WriteLine("Destruct instance of A"); } public void F() { Console.WriteLine("A.F"); TMest.RefA = this; } } class B { public A Ref; ~B() { Console.WriteLine("Destruct instance of B"); Ref.F(); } } class TMest { public static A RefA; public static B RefB; static void Main() { RefB = new B(); RefA = new A(); RefB.Ref = RefA; RefB = null; RefA = null; // A and B now eligible for destruction GC.Collect(); GC.WaitForPendingFinalizers(); // B now eligible for collection, but A is not if (RefA != null) Console.WriteLine("RefA is not null"); } } |
Destruct instance of A Destruct instance of B A.F RefA is not null |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。