http://www.itwis.com/html/net/c/20101115/9558.html

 C#集合类之Array,ArrayList,List<T>的用法,net有哪些集合类?array和ArrayList有什么区别?Hashtable与集合有什么不一样?....等等.这时才意识到,如果对.net本身提供的集合类不了解,根本不会明白引用Iesi.Collections的用意.
 由<<CLR via C#>>的书中所说:"所有的数组(如int[],FileStream[],object[])都隐式派生自System.Array,所有数组都隐式实现IEnumerable,ICollection,IList",所以先从Array开始研究,用Reflector工具找到Array源代码,发现除去一堆的静态方法如Sort,Find,BinarySearch以外,几乎就不剩什么东西了.
 
其中比较重要的一点是Array仅仅用显示接口实现了一个私有IList.Add方法,这意味着:Array实例没有办法调用Add方法,一旦初始化以后,长度不可变.
 


int IList.Add(object value){    throw new NotSupportedException(Environment.GetResourceString("NotSupported_FixedSizeCollection"));}

同样通过源代码可以看到ArrayList和Array的区别,ArrayList内置了一个Array变量 _items(代码中红色标出),
 
也就是说:ArrayList是Array的一层封装,实际数据操作还是针对Array.
 
[Serializable, ComVisible(true), DebuggerDisplay("Count = {Count}"), DebuggerTypeProxy(typeof(ArrayListDebugView))]public class ArrayList : IList, ICollection, IEnumerable, ICloneable{    // Fields    private const int _defaultCapacity = 4;    private object[] _items;    private int _size;    // Methods  ...........}

ArrayList有Add方法,当Add方法发现内部的object[]容量已满时,便会调用一个方法自动扩充object[]容量,既然ArrayList的实质是操作object[],而Array长度不可变,那么如何扩充?其实说白了,就是通过调用EnsureCapacity方法再创建一个更长的object[]数组,然后把原数组复制到新的数组中.
 
ArrayList的很多方法如Sort,Indexof,内部都是调用了Array的静态方法,如IndexOf方法:
 


public virtual int IndexOf(object value){    return Array.IndexOf(this._items, value, 0, this._size);}

.net2.0以后出现了泛型,目的是为了避免装箱拆箱操作造成的性能损失.ArrayList对应的泛型集合就是List<T>.
 
由于泛型的出现,List<T>内部操作的不再是object[],而是T[],提供的很多方法如Sort,IndexOf等,同ArrayList类一样,内部也是调用Array的静态方法来操作数组.
 
因为Array的局限性,List<T>的一些方法会用到循环,如Find方法:
 


public T Find(Predicate<T> match){    if (match == null)    {        ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);    }    for (int i = 0; i < this._size; i++)    {        if (match(this._items[i]))        {            return this._items[i];        }    }    return default(T);}
本文来自: IT知道网(http://www.itwis.com/) 详细出处参考:http://www.itwis.com/html/net/c/20110509/10281.html

posted on 2011-09-05 15:22  higirle  阅读(338)  评论(0编辑  收藏  举报