代码如下:
using System;
class Testing : IDisposable
{
    bool is_disposed = false;
    protected virtual void Dispose(bool disposing)
    {
        if (!is_disposed)  //only dispose once!
        {
            if (disposing)
            {
                Console.WriteLine( "Not in destructor, OK to reference other objects");
                //perform cleanup for this object
                Console.WriteLine( "Disposing....");
            }
            this.is_disposed = true;
        }        public void Disposed( )  //这里重载鸟
        {
            Dispose(true);
            //tell the GC not to finalize
            GC.SuppressFinalize(this);
        }        ~Testing ( )
        {  
            Dispose(false);
            Console.WriteLine("In destructor.");
        }

解决方案 »

  1.   

    代码有点错误,又无法修改,重写如下
    using System; 
    class Testing : IDisposable 

        bool is_disposed = false; 
        protected virtual void Dispose(bool disposing) 
        { 
            if (!is_disposed) //only dispose once! 
            { 
                if (disposing) 
                { 
                    Console.WriteLine(   "Not   in   destructor,   OK   to   reference   other   objects"); 
                }
                //perform cleanup for this object 
                Console.WriteLine( "Disposing...."); 
            } 
                this.is_disposed   =   true; 
        }     public void Disposed()     //这里重载鸟 
        { 
            Dispose(true); 
            //tell   the   GC   not   to   finalize 
            GC.SuppressFinalize(this); 
        }     ~Testing () 
        {     
            Dispose(false); 
            Console.WriteLine("In   destructor."); 
        } 
    }为什么要先实现一个protected,然后再实现一个public的?
      

  2.   

    会不会是这样,声明为可以重写的Dispose方法是供Testing子类调用或重写的而不可以重写的那个公开Dispose方法   则是为了供 Testing类的实例调用的。
      

  3.   

    Dispose(bool disposing)这个方法是为了区分是垃圾回收还是手动释放。简单的说,如果是手动释放,那就需要释放该对象申请的所有资源。如果是垃圾回收,就不需要。详细的去看下Control控件的dispose说明。
    这种例子有还不如没有,纯粹是为了演示而演示的,啥都说明不了。
      

  4.   

    这个类子的作用就是告诉你,你写一个类可以继承IDisposable 接口,然后可以自己手动释放该类生成的对象占用的空间
      

  5.   

    danjiewu大侠一语点破啊,高人。