理解析构函数的执行过程

 

Code
class First
    {
        
public First()
        {
            System.Console.WriteLine(
"First class start!");
        }
        
~First()
        {
            System.Console.WriteLine(
"First's destructor is called");
            System.Console.ReadLine();
        }
    }

    
class Second : First
    {
        
public Second()
        {
            System.Console.WriteLine(
"Second class start!");
        }
        
~Second()
        {
            System.Console.WriteLine(
"Second's destructor is called");
        }
    }

    
class Third : Second
    {
        
public Third()
        {
            System.Console.WriteLine(
"Third class start!");
        }
        
~Third()
        {
            System.Console.WriteLine(
"Third's destructor is called");
        }
    }

    
class TestDestructors
    {
        
static void Main()
        {
            Third t 
= new Third();
        }
    }

 

 

First class start!

Second class start!

Third class start!

Third's destructor is called

Second's destructor is called

First's destructor is called

 

析构函数的执行过程实际是执行了Finalize方法,具体的方法实际是:

protected overrid Finalize()

{

  try

    {

      //implemention

    }

  Finally

    {

       base.Finalize();

    }

}

posted @ 2009-06-10 15:20  chunchill  阅读(286)  评论(3编辑  收藏  举报