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

本文详细介绍了C#中的泛型集合List<T>的实现原理与使用方法,对比了非泛型集合ArrayList在处理值类型时的装箱拆箱操作带来的性能开销,并通过具体代码示例展示了List<T>如何避免这一问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

  1. ArrayList myArrayList = new ArrayList();  
  2.   
  3. myArrayList.Add(40);//装箱   
  4.   
  5. myArrayList.Add(80);//装箱   
  6.   
  7. Int32 a1 = (Int32)myArrayList[0];//拆箱   
  8.   
  9. Int32 a2 = (Int32)myArrayList[1];//拆箱  
            ArrayList myArrayList = new ArrayList();

            myArrayList.Add(40);//装箱

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

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

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

 

  1. /// <summary>   
  2.    /// 泛型集合类   
  3.    /// </summary>   
  4.    /// <typeparam name="T"></typeparam>   
  5.    public class List<T> : IList<T>, IList  
  6.    {  
  7.        /// <summary>   
  8.        /// 泛型迭代器   
  9.        /// </summary>   
  10.        /// <typeparam name="T"></typeparam>   
  11.        public struct Enumertor<T> : IEnumerator, IEnumerator<T>  
  12.        {  
  13.            //迭代索引   
  14.            private int index;  
  15.   
  16.            //迭代器所属的集合对象引用   
  17.            private List<T> list;  
  18.   
  19.            public Enumertor(List<T> container)  
  20.            {  
  21.                this.list = container;  
  22.   
  23.                this.index = -1;  
  24.            }  
  25.   
  26.            public void Dispose()  
  27.            {  
  28.            }  
  29.   
  30.           /// <summary>   
  31.           /// 显示实现IEnumerator的Current属性   
  32.           /// </summary>   
  33.            object IEnumerator.Current  
  34.            {  
  35.                get  
  36.                {  
  37.                    return list[index];  
  38.                }  
  39.            }  
  40.   
  41.            /// <summary>   
  42.            /// 实现IEnumerator<T>的Current属性   
  43.            /// </summary>   
  44.            public T Current  
  45.            {  
  46.                get  
  47.                {  
  48.                    return list[index];  
  49.                }  
  50.            }  
  51.   
  52.            /// <summary>   
  53.            /// 迭代器指示到下一个数据位置   
  54.            /// </summary>   
  55.            /// <returns></returns>   
  56.            public bool MoveNext()  
  57.            {  
  58.                if (this.index < list.Count)  
  59.                {  
  60.                    ++this.index;  
  61.                }  
  62.   
  63.                return this.index < list.Count;  
  64.            }  
  65.   
  66.            public void Reset()  
  67.            {  
  68.                this.index = -1;  
  69.            }  
  70.        }  
  71.   
  72.        /// <summary>   
  73.        /// 保存数据的数组,T类型则体现了泛型的作用。   
  74.        /// </summary>   
  75.        private T[] array;  
  76.   
  77.        /// <summary>   
  78.        /// 当前集合的长度   
  79.        /// </summary>   
  80.        private int count;  
  81.   
  82.        /// <summary>   
  83.        /// 默认构造函数   
  84.        /// </summary>   
  85.        public List()  
  86.            : this(1)  
  87.        {  
  88.   
  89.        }  
  90.   
  91.        public List(int capacity)  
  92.        {  
  93.            if (capacity < 0)  
  94.            {  
  95.                throw new Exception("集合初始长度不能小于0");  
  96.            }  
  97.   
  98.            if (capacity == 0)  
  99.            {  
  100.                capacity = 1;  
  101.            }  
  102.   
  103.            this.array = new T[capacity];  
  104.        }  
  105.   
  106.        /// <summary>   
  107.        /// 集合长度   
  108.        /// </summary>   
  109.        public int Count  
  110.        {  
  111.            get  
  112.            {  
  113.                return this.count;  
  114.            }  
  115.        }  
  116.   
  117.        /// <summary>   
  118.        /// 集合实际长度   
  119.        /// </summary>   
  120.        public int Capacity  
  121.        {  
  122.            get  
  123.            {  
  124.                return this.array.Length;  
  125.            }  
  126.        }  
  127.   
  128.        /// <summary>   
  129.        /// 是否固定大小   
  130.        /// </summary>   
  131.        public bool IsFixedSize  
  132.        {  
  133.            get  
  134.            {  
  135.                return false;  
  136.            }  
  137.        }  
  138.   
  139.        /// <summary>   
  140.        /// 是否只读   
  141.        /// </summary>   
  142.        public bool IsReadOnly  
  143.        {  
  144.            get  
  145.            {  
  146.                return false;  
  147.            }  
  148.        }  
  149.   
  150.        /// <summary>   
  151.        /// 是否可同属性   
  152.        /// </summary>   
  153.        public bool IsSynchronized  
  154.        {  
  155.            get  
  156.            {  
  157.                return false;  
  158.            }  
  159.        }  
  160.   
  161.        /// <summary>   
  162.        /// 同步对象   
  163.        /// </summary>   
  164.        public object SyncRoot  
  165.        {  
  166.            get  
  167.            {  
  168.                return null;  
  169.            }  
  170.        }  
  171.   
  172.        /// <summary>   
  173.        /// 长度不够时,重新分配长度足够的数组   
  174.        /// </summary>   
  175.        /// <returns></returns>   
  176.        private T[] GetNewArray()  
  177.        {  
  178.            return new T[(this.array.Length + 1) * 2];  
  179.        }  
  180.   
  181.        /// <summary>   
  182.        /// 实现IList<T>Add方法   
  183.        /// </summary>   
  184.        /// <param name="value"></param>   
  185.        public void Add(T value)  
  186.        {  
  187.            int newCount = this.count + 1;  
  188.   
  189.            if (this.array.Length < newCount)  
  190.            {  
  191.                T[] newArray = GetNewArray();  
  192.   
  193.                Array.Copy(this.array, newArray, this.count);  
  194.   
  195.                this.array = newArray;  
  196.            }  
  197.   
  198.            this.array[this.count] = value;  
  199.   
  200.            this.count = newCount;  
  201.        }  
  202.   
  203.        /// <summary>   
  204.        /// 向集合末尾添加对象   
  205.        /// </summary>   
  206.        /// <param name="value"></param>   
  207.        /// <returns></returns>   
  208.         int IList.Add(object value)  
  209.        {  
  210.            ((IList<T>)this).Add((T)value);  
  211.   
  212.            return this.count - 1;  
  213.        }  
  214.   
  215.        /// <summary>   
  216.        /// 实现IList<T>索引器   
  217.        /// </summary>   
  218.        /// <param name="index"></param>   
  219.        /// <returns></returns>   
  220.        public T this[int index]  
  221.        {  
  222.            get  
  223.            {  
  224.                if (index < 0 || index >= this.count)  
  225.                {  
  226.                    throw new ArgumentOutOfRangeException("index");  
  227.                }  
  228.   
  229.                return this.array[index];  
  230.            }  
  231.   
  232.            set  
  233.            {  
  234.                if (index < 0 || index >= this.count)  
  235.                {  
  236.                    throw new ArgumentOutOfRangeException("index");  
  237.                }  
  238.   
  239.                this.array[index] = value;  
  240.            }  
  241.        }  
  242.   
  243.        /// <summary>   
  244.        /// 显示实现IList接口的索引器   
  245.        /// </summary>   
  246.        /// <param name="index"></param>   
  247.        /// <returns></returns>   
  248.        object IList.this[int index]  
  249.        {  
  250.            get  
  251.            {  
  252.                return ((IList<T>)this)[index];  
  253.            }  
  254.   
  255.            set  
  256.            {  
  257.                ((IList<T>)this)[index] = (T)value;  
  258.            }  
  259.        }  
  260.   
  261.        /// <summary>   
  262.        /// 删除集合中的元素   
  263.        /// </summary>   
  264.        /// <param name="index"></param>   
  265.        /// <param name="count"></param>   
  266.        public void RemoveRange(int index, int count)  
  267.        {  
  268.            if (index < 0)  
  269.            {  
  270.                throw new ArgumentOutOfRangeException("index");  
  271.            }  
  272.   
  273.            int removeIndex = index + count;  
  274.   
  275.            if (count < 0 || removeIndex > this.count)  
  276.            {  
  277.                throw new ArgumentOutOfRangeException("index");  
  278.            }  
  279.   
  280.            Array.Copy(this.array, index + 1, this.array, index + count - 1, this.count - removeIndex);  
  281.   
  282.            this.count -= count;  
  283.        }  
  284.   
  285.        /// <summary>   
  286.        /// 实现IList<T>接口的indexOf方法   
  287.        /// </summary>   
  288.        /// <param name="value"></param>   
  289.        /// <returns></returns>   
  290.        public int IndexOf(T value)  
  291.        {  
  292.            int index = 0;  
  293.   
  294.            if (value == null)  
  295.            {  
  296.                while (index < this.count)  
  297.                {  
  298.                    if (this.array[index] == null)  
  299.                    {  
  300.                        return index;  
  301.                    }  
  302.   
  303.                    ++index;  
  304.                }  
  305.            }  
  306.            else  
  307.            {  
  308.                while (index < this.count)  
  309.                {  
  310.                    if (value.Equals(this.array[index]))  
  311.                    {  
  312.                        return index;  
  313.                    }  
  314.   
  315.                    ++index;  
  316.                }  
  317.            }  
  318.            return -1;  
  319.        }  
  320.   
  321.        /// <summary>   
  322.        /// 显示实现IList接口的IndexOf方法   
  323.        /// </summary>   
  324.        /// <param name="value"></param>   
  325.        /// <returns></returns>   
  326.         int IList.IndexOf(object value)  
  327.        {  
  328.            return ((IList<T>)this).IndexOf((T)value);  
  329.        }  
  330.   
  331.        /// <summary>   
  332.        /// 查找对应数组项   
  333.        /// </summary>   
  334.        /// <param name="o"></param>   
  335.        /// <param name="compar"></param>   
  336.        /// <returns></returns>   
  337.        public int IndexOf(object o, IComparer compar)  
  338.        {  
  339.            int index = 0;  
  340.   
  341.            while (index < this.count)  
  342.            {  
  343.                if (compar.Compare(this.array[index], o) == 0)  
  344.                {  
  345.                    return index;  
  346.                }  
  347.   
  348.                ++index;  
  349.            }  
  350.   
  351.            return -1;  
  352.        }  
  353.   
  354.        /// <summary>   
  355.        /// 实现IList<T>接口的Remove方法   
  356.        /// </summary>   
  357.        /// <param name="value"></param>   
  358.        /// <returns></returns>   
  359.        public bool Remove(T value)  
  360.        {  
  361.            int index = this.IndexOf(value);  
  362.   
  363.            if (index >= 0)  
  364.            {  
  365.                this.RemoveRange(index, 1);  
  366.   
  367.                return true;  
  368.            }  
  369.   
  370.            return false;  
  371.        }  
  372.   
  373.        /// <summary>   
  374.        /// 显示实现IList接口的Remove方法,此处显示实现   
  375.        /// </summary>   
  376.        /// <param name="value"></param>   
  377.        void IList.Remove(object value)  
  378.        {  
  379.            ((IList<T>)this).Remove((T)value);  
  380.        }  
  381.   
  382.        /// <summary>   
  383.        /// 从集合指定位置删除对象的引用   
  384.        /// </summary>   
  385.        /// <param name="index"></param>   
  386.        public void RemoveAt(int index)  
  387.        {  
  388.            RemoveRange(index, 1);  
  389.        }  
  390.   
  391.        /// <summary>   
  392.        /// 弹出集合的最后一个元素   
  393.        /// </summary>   
  394.        /// <returns></returns>   
  395.        public object PopBack()  
  396.        {  
  397.            object o = this.array[this.count - 1];  
  398.   
  399.            RemoveAt(this.count - 1);  
  400.   
  401.            return o;  
  402.        }  
  403.   
  404.        /// <summary>   
  405.        /// 弹出集合第一个对象   
  406.        /// </summary>   
  407.        /// <returns></returns>   
  408.        public object PopFront()  
  409.        {  
  410.            object o = this.array[0];  
  411.   
  412.            RemoveAt(0);  
  413.   
  414.            return o;  
  415.        }  
  416.   
  417.        /// <summary>   
  418.        /// 实现IList<T>接口的Insert方法   
  419.        /// </summary>   
  420.        /// <param name="index"></param>   
  421.        /// <param name="value"></param>   
  422.        public void Insert(int index, T value)  
  423.        {  
  424.            if (index >= this.count)  
  425.            {  
  426.                throw new ArgumentOutOfRangeException("index");  
  427.            }  
  428.   
  429.            int newCount = this.count + 1;  
  430.   
  431.            if (this.array.Length < newCount)  
  432.            {  
  433.                T[] newArray = GetNewArray();  
  434.   
  435.                Array.Copy(this.array, newArray, index);  
  436.   
  437.                newArray[index] = value;  
  438.   
  439.                Array.Copy(this.array, index, newArray, index + 1, this.count - index);  
  440.   
  441.                this.array = newArray;  
  442.            }  
  443.            else  
  444.            {  
  445.                Array.Copy(this.array, index, this.array, index + 1, this.count - index);  
  446.   
  447.                this.array[index] = value;  
  448.            }  
  449.   
  450.            this.count = newCount;  
  451.        }  
  452.   
  453.        /// <summary>   
  454.        /// 显示实现IList接口的Insert方法   
  455.        /// </summary>   
  456.        /// <param name="index"></param>   
  457.        /// <param name="value"></param>   
  458.        void IList.Insert(int index, object value)  
  459.        {  
  460.            ((IList<T>)this).Insert(index, (T)value);  
  461.        }  
  462.   
  463.        /// <summary>   
  464.        /// 实现IList<T>接口的Contains方法   
  465.        /// </summary>   
  466.        /// <param name="value"></param>   
  467.        /// <returns></returns>   
  468.        public bool Contains(T value)  
  469.        {  
  470.            return this.IndexOf(value) >= 0;  
  471.        }  
  472.   
  473.        /// <summary>   
  474.        /// 显示实现IList<T>接口的Contains方法   
  475.        /// </summary>   
  476.        /// <param name="value"></param>   
  477.        /// <returns></returns>   
  478.        bool IList.Contains(object value)  
  479.        {  
  480.            return ((IList<T>)this).IndexOf((T)value) >= 0;  
  481.        }  
  482.   
  483.        /// <summary>   
  484.        /// 将集合压缩为实际长度   
  485.        /// </summary>   
  486.        public void TrimToSize()  
  487.        {  
  488.            if (this.array.Length > this.count)  
  489.            {  
  490.                T[] newArray = null;  
  491.   
  492.                if (this.count > 0)  
  493.                {  
  494.                    newArray = new T[this.count];  
  495.   
  496.                    Array.Copy(this.array, newArray, this.count);  
  497.                }  
  498.                else  
  499.                {  
  500.                    newArray = new T[1];  
  501.                }  
  502.   
  503.                this.array = newArray;  
  504.            }  
  505.        }  
  506.   
  507.        /// <summary>   
  508.        /// 清空集合   
  509.        /// </summary>   
  510.        public void Clear()  
  511.        {  
  512.            this.count = 0;  
  513.        }  
  514.   
  515.        /// <summary>   
  516.        /// 实现IEnumerable接口的GetEnumerator方法   
  517.        /// </summary>   
  518.        /// <returns></returns>   
  519.        public IEnumerator<T> GetEnumerator()  
  520.        {  
  521.            Enumertor<T> ator = new Enumertor<T>(this);  
  522.   
  523.            return ator;  
  524.        }  
  525.   
  526.        /// <summary>   
  527.        /// 显示实现IEnumerable接口的GetEnumerator方法   
  528.        /// </summary>   
  529.        /// <returns></returns>   
  530.        IEnumerator IEnumerable.GetEnumerator()  
  531.        {  
  532.            return ((IEnumerable<T>)this).GetEnumerator();  
  533.        }  
  534.   
  535.        /// <summary>   
  536.        /// 实现ICollection<T>接口的CopyTo方法   
  537.        /// </summary>   
  538.        /// <param name="array"></param>   
  539.        /// <param name="index"></param>   
  540.        public void CopyTo(T[] array, int index)  
  541.        {  
  542.            Array.Copy(this.array, 0, array, index, this.count);  
  543.        }  
  544.   
  545.        /// <summary>   
  546.        /// 显示实现实现ICollection<T>接口的CopyTo方法   
  547.        /// </summary>   
  548.        /// <param name="array"></param>   
  549.        /// <param name="index"></param>   
  550.        void ICollection.CopyTo(Array array, int index)  
  551.        {  
  552.            Array.Copy(this.array, 0, array, index, this.count);  
  553.        }  
  554.    }  
 /// <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;
            }
        }

        /// <summary>
        /// 同步对象
        /// </summary>
        public object SyncRoot
        {
            get
            {
                return null;
            }
        }

        /// <summary>
        /// 长度不够时,重新分配长度足够的数组
        /// </summary>
        /// <returns></returns>
        private T[] GetNewArray()
        {
            return new T[(this.array.Length + 1) * 2];
        }

        /// <summary>
        /// 实现IList<T>Add方法
        /// </summary>
        /// <param name="value"></param>
        public void Add(T value)
        {
            int newCount = this.count + 1;

            if (this.array.Length < newCount)
            {
                T[] newArray = GetNewArray();

                Array.Copy(this.array, newArray, this.count);

                this.array = newArray;
            }

            this.array[this.count] = value;

            this.count = newCount;
        }

        /// <summary>
        /// 向集合末尾添加对象
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
         int IList.Add(object value)
        {
            ((IList<T>)this).Add((T)value);

            return this.count - 1;
        }

        /// <summary>
        /// 实现IList<T>索引器
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public T this[int index]
        {
            get
            {
                if (index < 0 || index >= this.count)
                {
                    throw new ArgumentOutOfRangeException("index");
                }

                return this.array[index];
            }

            set
            {
                if (index < 0 || index >= this.count)
                {
                    throw new ArgumentOutOfRangeException("index");
                }

                this.array[index] = value;
            }
        }

        /// <summary>
        /// 显示实现IList接口的索引器
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        object IList.this[int index]
        {
            get
            {
                return ((IList<T>)this)[index];
            }

            set
            {
                ((IList<T>)this)[index] = (T)value;
            }
        }

        /// <summary>
        /// 删除集合中的元素
        /// </summary>
        /// <param name="index"></param>
        /// <param name="count"></param>
        public void RemoveRange(int index, int count)
        {
            if (index < 0)
            {
                throw new ArgumentOutOfRangeException("index");
            }

            int removeIndex = index + count;

            if (count < 0 || removeIndex > this.count)
            {
                throw new ArgumentOutOfRangeException("index");
            }

            Array.Copy(this.array, index + 1, this.array, index + count - 1, this.count - removeIndex);

            this.count -= count;
        }

        /// <summary>
        /// 实现IList<T>接口的indexOf方法
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public int IndexOf(T value)
        {
            int index = 0;

            if (value == null)
            {
                while (index < this.count)
                {
                    if (this.array[index] == null)
                    {
                        return index;
                    }

                    ++index;
                }
            }
            else
            {
                while (index < this.count)
                {
                    if (value.Equals(this.array[index]))
                    {
                        return index;
                    }

                    ++index;
                }
            }
            return -1;
        }

        /// <summary>
        /// 显示实现IList接口的IndexOf方法
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
         int IList.IndexOf(object value)
        {
            return ((IList<T>)this).IndexOf((T)value);
        }

        /// <summary>
        /// 查找对应数组项
        /// </summary>
        /// <param name="o"></param>
        /// <param name="compar"></param>
        /// <returns></returns>
        public int IndexOf(object o, IComparer compar)
        {
            int index = 0;

            while (index < this.count)
            {
                if (compar.Compare(this.array[index], o) == 0)
                {
                    return index;
                }

                ++index;
            }

            return -1;
        }

        /// <summary>
        /// 实现IList<T>接口的Remove方法
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool Remove(T value)
        {
            int index = this.IndexOf(value);

            if (index >= 0)
            {
                this.RemoveRange(index, 1);

                return true;
            }

            return false;
        }

        /// <summary>
        /// 显示实现IList接口的Remove方法,此处显示实现
        /// </summary>
        /// <param name="value"></param>
        void IList.Remove(object value)
        {
            ((IList<T>)this).Remove((T)value);
        }

        /// <summary>
        /// 从集合指定位置删除对象的引用
        /// </summary>
        /// <param name="index"></param>
        public void RemoveAt(int index)
        {
            RemoveRange(index, 1);
        }

        /// <summary>
        /// 弹出集合的最后一个元素
        /// </summary>
        /// <returns></returns>
        public object PopBack()
        {
            object o = this.array[this.count - 1];

            RemoveAt(this.count - 1);

            return o;
        }

        /// <summary>
        /// 弹出集合第一个对象
        /// </summary>
        /// <returns></returns>
        public object PopFront()
        {
            object o = this.array[0];

            RemoveAt(0);

            return o;
        }

        /// <summary>
        /// 实现IList<T>接口的Insert方法
        /// </summary>
        /// <param name="index"></param>
        /// <param name="value"></param>
        public void Insert(int index, T value)
        {
            if (index >= this.count)
            {
                throw new ArgumentOutOfRangeException("index");
            }

            int newCount = this.count + 1;

            if (this.array.Length < newCount)
            {
                T[] newArray = GetNewArray();

                Array.Copy(this.array, newArray, index);

                newArray[index] = value;

                Array.Copy(this.array, index, newArray, index + 1, this.count - index);

                this.array = newArray;
            }
            else
            {
                Array.Copy(this.array, index, this.array, index + 1, this.count - index);

                this.array[index] = value;
            }

            this.count = newCount;
        }

        /// <summary>
        /// 显示实现IList接口的Insert方法
        /// </summary>
        /// <param name="index"></param>
        /// <param name="value"></param>
        void IList.Insert(int index, object value)
        {
            ((IList<T>)this).Insert(index, (T)value);
        }

        /// <summary>
        /// 实现IList<T>接口的Contains方法
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool Contains(T value)
        {
            return this.IndexOf(value) >= 0;
        }

        /// <summary>
        /// 显示实现IList<T>接口的Contains方法
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        bool IList.Contains(object value)
        {
            return ((IList<T>)this).IndexOf((T)value) >= 0;
        }

        /// <summary>
        /// 将集合压缩为实际长度
        /// </summary>
        public void TrimToSize()
        {
            if (this.array.Length > this.count)
            {
                T[] newArray = null;

                if (this.count > 0)
                {
                    newArray = new T[this.count];

                    Array.Copy(this.array, newArray, this.count);
                }
                else
                {
                    newArray = new T[1];
                }

                this.array = newArray;
            }
        }

        /// <summary>
        /// 清空集合
        /// </summary>
        public void Clear()
        {
            this.count = 0;
        }

        /// <summary>
        /// 实现IEnumerable接口的GetEnumerator方法
        /// </summary>
        /// <returns></returns>
        public IEnumerator<T> GetEnumerator()
        {
            Enumertor<T> ator = new Enumertor<T>(this);

            return ator;
        }

        /// <summary>
        /// 显示实现IEnumerable接口的GetEnumerator方法
        /// </summary>
        /// <returns></returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return ((IEnumerable<T>)this).GetEnumerator();
        }

        /// <summary>
        /// 实现ICollection<T>接口的CopyTo方法
        /// </summary>
        /// <param name="array"></param>
        /// <param name="index"></param>
        public void CopyTo(T[] array, int index)
        {
            Array.Copy(this.array, 0, array, index, this.count);
        }

        /// <summary>
        /// 显示实现实现ICollection<T>接口的CopyTo方法
        /// </summary>
        /// <param name="array"></param>
        /// <param name="index"></param>
        void ICollection.CopyTo(Array array, int index)
        {
            Array.Copy(this.array, 0, array, index, this.count);
        }
    }

调用:

  1. static void Main(string[] args)  
  2.        {  
  3.            //由于已经指定了int,因此加入值类型不会有装箱拆箱操作。   
  4.            List<int> tList = new List<int>();  
  5.   
  6.            tList.Add(25);  
  7.   
  8.            tList.Add(30);  
  9.   
  10.            foreach (int n in tList)  
  11.            {  
  12.                Console.WriteLine(n);  
  13.            }  
  14.   
  15.            Console.ReadLine();  
  16.        }  
 static void Main(string[] args)
        {
            //由于已经指定了int,因此加入值类型不会有装箱拆箱操作。
            List<int> tList = new List<int>();

            tList.Add(25);

            tList.Add(30);

            foreach (int n in tList)
            {
                Console.WriteLine(n);
            }

            Console.ReadLine();
        }

 代码:http://download.youkuaiyun.com/detail/yysyangyangyangshan/4787961

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值