class A
    {
        ~A() { Console.WriteLine("调用类A的析构函数"); }
    }
    class B : A
    {
        ~B() { Console.WriteLine("调用类B的析构函数"); }
    }
    class C : B
    {
        ~C() { Console.WriteLine("调用类C的析构函数"); }
    }    class Program
    {
        static void Main(string[] args)
        {
            C b = new C();
            Console.ReadKey();
        }
    }在vs2008中运行没结果,是析构函数格式错了吗?还是其他原因

解决方案 »

  1.   

    static void Main(string[] args)
      {
      C b = new C();
      Console.ReadKey();
      }
     无语  你这里明显就只有读取 输出呢?回车以后你要做什么操作,要输出什么?
      

  2.   

    析构函数系统会自动调用的
     你的C类并没有销毁,所以并不会输出的。
    你可以尝试当用户按下Enter键后,设置C为null值,就会输出了。class A
    {
          A() {Console.WriteLine("调用类A的构造函数");}
         ~A() { Console.WriteLine("调用类A的析构函数"); }
    }
    class B : A
    {
         B() {Console.WriteLine("调用类B的构造函数");}
         ~B() { Console.WriteLine("调用类B的析构函数"); }
    }
    class C : B
    {
        C() {Console.WriteLine("调用类C的构造函数");}
        ~C() { Console.WriteLine("调用类C的析构函数"); }
    }
    class Program
    {
        static void Main(string[] args)
        {
            C c = new C();
            Console.WriteLine("Press enter to Destroy it");  
            Console.ReadLine();  
            c=null;  
            Console.Read();  
        }
    }
    这样运行就可以看出整个过程了