.NET Tip: Implement IDisposable to Ensure Your Objects Clean Up After Themselves

11880 ワード

public class GoodResourceCitizen : IDisposable

{

    private bool _IsDisposed = false;

    ~GoodResourceCitizen()

    {

        Dispose(false);

    }

    public void Dispose()

    {

        Dispose(true);

        // Tell the garbage collector not to call the finalizer

        // since all the cleanup will already be done.

        GC.SuppressFinalize(true);

    }

    protected virtual void Dispose(bool IsDisposing)

    {

        if (_IsDisposed)

          return;

       if (IsDisposing)

       {

           // Free any managed resources in this section

       }

       // Free any unmanaged resources in this section

       _IsDisposed = true;

    }

}

 
 
 public class Person:IDisposable

    {

        public int Age { get; set; }



        public override string ToString()

        {



            return base.ToString();

        }

        public Person ShallowClone()

        {



            return base.MemberwiseClone() as Person;

        }



        public Person DeepClone()

        {

            var p = new Person();

            p.Age = this.Age;

            p.Address = new Address() { Detail = this.Address.Detail };

            return p;



        }

        /// <summary>

        ///  

        ///  Finalize() 

        /// </summary>

        ~Person()

        {

            // base.Finalize(); 

            Dispose(false);

        }





        public Address Address { get; set; }



        public void Dispose()

        {

            Dispose(true);

            // Dispose(true) , GC Finalize() , Finalize()

            GC.SuppressFinalize(this);

        }

        /// <summary>

        /// 

        /// </summary>

        /// <param name="disposing"> Dispose </param>

        protected virtual void Dispose(bool disposing)

        {

            if (disposing)

            {

                //release managed resources

            }

            //release unmanaged resources

        }

        

    }



using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;



namespace ConsoleApplication1

{

    public class Human:IDisposable

    {

        ~Human()

        {

            Dispose(false);

     

        }



        public void Dispose()

        {

            Dispose(true);

            GC.SuppressFinalize(this);

        }

        protected virtual void Dispose(bool disposing)

        {

            if (disposing)

            {

                //release managed resources

            }



            //release unmanaged resources



        }

    }



    public class Animal 

    {

        ~Animal()

        {

            Dispose(false);



        }



        public void IDisposable.Dispose()

        {

            Dispose(true);

            GC.SuppressFinalize(this);

        }

        protected virtual void Dispose(bool disposing)

        {

            if (disposing)

            {

                //release managed resources

            }



            //release unmanaged resources



        }

        public void Close()

        {

            Dispose(false);

        }

    }



    public class demo

    {

        public void haha()

        {

            var human = new Human();

            human.Dispose();



            var animal = new Animal();

            animal.Close();



            (animal as IDisposable).Dispose();

        }

    }

}

 
参考文献
1. C#におけるFinalizeメソッドの問題
2. Object.Finalizeメソッド
3. C#におけるFinalizeメソッドの問題
4. C#におけるオブジェクトの破棄には,Finalize,Dispose,GCの3つの方式がある.
5. 『Effective C#』読書ノート——了解.NETメモリ管理メカニズム<.NETリソース管理>