Inheritance and Destructors in C#

后端 未结 7 999
灰色年华
灰色年华 2021-02-13 19:30

According to this, it states that Destructors cannot be inherited or overloaded. In my case, for all subclasses, the destructors will be identical. Is this pretty m

7条回答
  •  悲&欢浪女
    2021-02-13 19:59

    A quick console app can help test this sort of thing.

    using System;
    
    class A
    {
        ~A() => Console.WriteLine("~A");       
    }
    
    class B : A
    {
        ~B() => Console.WriteLine("~B");
    }
    
    public class Program
    {
        public static void Main() => new B();        
    }
    

    The output might be...

    ~B
    ~A
    

提交回复
热议问题