C#索引器

        private T[] _list;
        public void Init(int maxCapcity = 100)
        {
            if (maxCapcity <= 0) throw new ArgumentException("maxCapcity无效");
            _maxCapcity = maxCapcity;
            _list = new T[maxCapcity];
        }

        private int _length;
        public int Lenght
        {
            get { return _length; }
        }

        private int _maxCapcity;
        public int MaxCapcity
        {
            get { return _maxCapcity; }
        }

        public T this[int index]
        {
            get
            {
                if (index >= Lenght) throw new IndexOutOfRangeException("index");
                return _list[index];
            }
            set
            {
                if (index >= MaxCapcity) throw new IndexOutOfRangeException("index");
                _list[index] = value;
            }
        }

 

posted @ 2014-12-14 17:36  飞不起来的胖子  阅读(187)  评论(0)    收藏  举报