日期:2014-05-17  浏览次数:20737 次

C#基础知识整理 基础知识(17)ILiest接口——泛型

对于ArrayList中如果插入值类型会引发装箱操作,而取出值类型又需要拆箱,如下

            ArrayList myArrayList = new ArrayList();

            myArrayList.Add(40);//装箱

            myArrayList.Add(80);//装箱
            
            Int32 a1 = (Int32)myArrayList[0];//拆箱

            Int32 a2 = (Int32)myArrayList[1];//拆箱

从而造成性能的消耗。至于装箱的详细解说见下一篇。
为了解决这些问题,C#中有支持泛型的IList<T>接口,下面看详细代码,其实结构都和IList一样,只是增加了泛型。

 

 /// <summary>
    /// 泛型集合类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class List<T> : IList<T>, IList
    {
        /// <summary>
        /// 泛型迭代器
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public struct Enumertor<T> : IEnumerator, IEnumerator<T>
        {
            //迭代索引
            private int index;

            //迭代器所属的集合对象引用
            private List<T> list;

            public Enumertor(List<T> container)
            {
                this.list = container;

                this.index = -1;
            }

            public void Dispose()
            {
            }

           /// <summary>
           /// 显示实现IEnumerator的Current属性
           /// </summary>
            object IEnumerator.Current
            {
                get
                {
                    return list[index];
                }
            }

            /// <summary>
            /// 实现IEnumerator<T>的Current属性
            /// </summary>
            public T Current
            {
                get
                {
                    return list[index];
                }
            }

            /// <summary>
            /// 迭代器指示到下一个数据位置
            /// </summary>
            /// <returns></returns>
            public bool MoveNext()
            {
                if (this.index < list.Count)
                {
                    ++this.index;
                }

                return this.index < list.Count;
            }

            public void Reset()
            {
                this.index = -1;
            }
        }

        /// <summary>
        /// 保存数据的数组,T类型则体现了泛型的作用。
        /// </summary>
        private T[] array;

        /// <summary>
        /// 当前集合的长度
        /// </summary>
        private int count;

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public List()
            : this(1)
        {

        }

        public List(int capacity)
        {
            if (capacity < 0)
            {
                throw new Exception("集合初始长度不能小于0");
            }

            if (capacity == 0)
            {
                capacity = 1;
            }

            this.array = new T[capacity];
        }

        /// <summary>
        /// 集合长度
        /// </summary>
        public int Count
        {
            get
            {
                return this.count;
            }
        }

        /// <summary>
        /// 集合实际长度
        /// </summary>
        public int Capacity
        {
            get
            {
                return this.array.Length;
            }
        }

        /// <summary>
        /// 是否固定大小
        /// </summary>
        public bool IsFixedSize
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// 是否只读
        /// </summary>
        public bool IsReadOnly
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// 是否可同属性
        /// </summary>
        public bool IsSynchronized
        {
            get
            {
                return false;
            }
        }

        //