在 Visual C# .NET 中實現自訂集合

來源:互聯網
上載者:User
View Code

using System;
using System.Collections;

class TempClass
{
    public class CustomCollection : ICollection
    {

        private int[] intArr = { 1, 5, 9 };
        private int Ct;

        public CustomCollection()
        {
            Ct = 3;
        }
        void ICollection.CopyTo(Array myArr, int index)
        {

            foreach (int i in intArr)
            {
                myArr.SetValue(i, index);
                index = index + 1;
            }

        }
        IEnumerator IEnumerable.GetEnumerator()
        {
            return new Enumerator(intArr);
        }
        //The IsSynchronized Boolean property returns True if the 
        //collection is designed to be thread safe; otherwise, it returns False.
        bool ICollection.IsSynchronized
        {
            get
            {
                return false;
            }
        }

        //The SyncRoot property returns an object, which is used for synchronizing 
        //the collection.This returns the instance of the object or returns the 
        //SyncRoot of other collections if the collection contains other collections.
        // 
        object ICollection.SyncRoot
        {
            get
            {
                return this;
            }
        }

        //The Count read-only property returns the number 
        //of items in the collection.
        int ICollection.Count
        {
            get
            {
                return Ct;
            }
        }
    }
    public class Enumerator : IEnumerator
    {
        private int[] intArr;
        private int Cursor;
        public Enumerator(int[] intarr)
        {
            this.intArr = intarr;
            Cursor = -1;
        }
        void IEnumerator.Reset()
        {
            Cursor = -1;
        }
        bool IEnumerator.MoveNext()
        {
            if (Cursor < intArr.Length)
                Cursor++;

            return (!(Cursor == intArr.Length));
        }
        object IEnumerator.Current
        {
            get
            {
                if ((Cursor < 0) || (Cursor == intArr.Length))
                    throw new InvalidOperationException();
                return intArr[Cursor];
            }
        }
    }
    static void Main()
    {
        CustomCollection MyCol = new CustomCollection();

        foreach (object MyObj in MyCol)
            Console.WriteLine(MyObj.ToString());
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.