C#知識點總結系列:2、C#中IDisposable和IEnumerable、IEnumerator


  C#中如何合理的釋放非托管內存?在本文中我們將講解使用IDisposable釋放托管內存和非托管內存。

  A.首先需要讓類實現IDisposable接口,然后實現IDispose方法。

    A.a核心Disponse(bool isDisponse)

      1.此方法首先判斷isReadyDisposed(判斷是否第一次調用此核心方法),如果不是第一次調用則不做任何操作。

      2.再判斷是否是析構函數調用?如果是析構函數調用不釋放托管資源,其交由GC進行釋放,如果析構函數釋放托管資源可能之前GC釋放過,就會導致出現異常。此判斷內部釋放托管資源內存。

      3.釋放非托管資源,並且設置標志位isReadyDisposed=true.

  B.然后分釋放托管內存和非托管內存兩種情況進行內存釋放處理。

    B.a釋放非托管內存

      1.釋放非托管內存需要手動調用本類的Dispose()方法,此方法首先調用Dispose(true)手動釋放托管和非托管資源,然后調用GC.SuppressFinalize(this),讓GC不要再調用此對象的析構函數。

    B.b釋放托管內存

      1.釋放托管內存是由GC自動調用析構函數,析構函數內部調用Dispose(false)方法.此時只釋放非托管資源,而托管資源不管,由GC自行釋放。

  我們實現好的類代碼如下:

    public class IDisponseTest : IDisposable
    {
        private bool isReadyDisposed = false;

        ~IDisponseTest()
        {
            //析構函數調用時不釋放托管資源,因為交由GC進行釋放
            Disponse(false);
        }

        public void Dispose()
        {
            //用戶手動釋放托管資源和非托管資源
            Disponse(true);
            //用戶已經釋放了托管和非托管資源,所以不需要再調用析構函數
            GC.SuppressFinalize(this);
            
            //如果子類繼承此類時,需要按照如下寫法進行。
            //try
            //{
            //    Disponse(true);
            //}
            //finally
            //{
            //    base.Disponse();
            //}
        }

        public virtual void Disponse(bool isDisponse)
        {
            //isReadyDisposed是控制只有第一次調用Disponse才有效才需要釋放托管和非托管資源
            if (isReadyDisposed)
                return;
            if (isDisponse)
            {
                //析構函數調用時不釋放托管資源,因為交由GC進行釋放
                //如果析構函數釋放托管資源可能之前GC釋放過,就會導致出現異常

                //托管資源釋放
            }
            //非托管資源釋放
            isReadyDisposed = true;
        }
    }

   C#制作一個迭代器對象?使用IEnumerable、IEnumerator

  首先:讓類繼承IEnumerable和IEnumerator接口,此時此類會出現IEnumerable.GetEnumerator()方法和IEnumerator.Current屬性、IEnumerator.MoveNext(),IEnumerator.Reset()方法。

  其次:IEnumerator接口是對象遍歷的方法和屬性實現,而IEnumerable.GetEnumerator()方法是為了獲取IEnumerator對象。

  最后:我們看看迭代器代碼實現如下實例:

    class Program
    {
        static void Main(string[] args)
        {
            CubeEnum cubelist = new CubeEnum(50);
            foreach(Cube cube in cubelist)
            {
                Console.WriteLine("立方體長:" + cube.Length + ",寬" + cube.Width + ",高" + cube.Height);
            }
            Console.Read();
        }
    }
    //立方體,長、寬、高
    public class Cube
    {
        public int Length { get; set; }
        public int Width { get; set; }
        public int Height { get; set; }
    }
    /// <summary>
    /// 立方體迭代集合,繼承了IEnumerable和IEnumerator
    /// </summary>
    public class CubeEnum : IEnumerable, IEnumerator
    {
        //索引
        public int Index { get; set; }
        //立方體集合
        public Cube[] cubelist { get; set; }
        //初始化立方體集合
        public CubeEnum(int count)
        {
            this.Index = -1;
            cubelist = new Cube[count];
            for (int i = 0; i < count; i++)
            {
                cubelist[i] = new Cube();
                cubelist[i].Length = i * 10;
                cubelist[i].Width = i * 10;
                cubelist[i].Height = i * 10;
            }
        }
        //實現IEnumerable的 GetEnumerator() 方法獲得IEnumerator對象
        public IEnumerator GetEnumerator()
        {
            return (IEnumerator)this;
        }
        //當前Cube立方體
        public object Current
        {
            get { return cubelist[Index]; }
        }
        //往下一步移動
        public bool MoveNext()
        {
            Index++;
            if (Index < cubelist.Length)
            {
                return true;
            }
            return false;
        }
        //重置索引
        public void Reset()
        {
            Index = -1;
        }
    }

  本文講述的是C#基礎的應用,如有差錯,敬請斧正。

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM