扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
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领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者