C#集合--ICollection接口和IList接口

虽然列举接口提供了一个协议,用于向前的方式遍历集合,但它们没有提供一种机制来肯定集合的大小,经过索引访问集合的成员,搜索集合,或修改集合。为了实现这些功能,.NET Framework定义了ICollection,IList和IDictionary接口。每一个接口都有Generic的接口和非Generic的接口,请注意非Generic多数用于支持遗留代码。html

这些接口的继承挂关系以下图所示:程序员

image

Generic的接口和非Generic的接口之间的差距超出了你的预期,特别是ICollection和ICollection<T>。这是因为历史缘由形成的,由于Generic类型是C# 2.0才引入的,因此Generic吸收了非Generic接口的经验和教训,从而设计了与之不一样的但更优秀的接口。正式因为这个缘由,ICollection<T>并无派生自ICollection,一样地,IList<T>也没有派生自IList, IDictionary<TKey,TValue>也没有派生自IDinctionary。所以集合类能够同时实现Generic的接口和非Generic的接口。(事实上,集合类通常都实现了两个类型接口,好比class Collection<T>: IList<T>, IList,有好比List<T> : IList<T>, System.Collections.IList)。数组

 

ICollection<T>和ICollection

ICollection<T>是能够统计集合中对象的标准接口。该接口能够肯定集合的大小(Count),集合是否包含某个元素(Contains),复制集合到另一个数组(ToArray),集合是不是只读的(IsReadOnly)。若是一个集合是可编辑的,那么能够调用Add,Remove和Clear方法操做集合中的元素。由于该接口继承IEnumerable<T>,因此可使用foreach语句遍历集合。该接口的定义以下安全

public interface ICollection<T> : IEnumerable<T>
{
    // Number of items in the collections.        
    int Count { get; }

    bool IsReadOnly { get; }

    void Add(T item);

    void Clear();

    bool Contains(T item); 
            
    // CopyTo copies a collection into an Array, starting at a particular
    // index into the array.
    // 
    void CopyTo(T[] array, int arrayIndex);
            
    //void CopyTo(int sourceIndex, T[] destinationArray, int destinationIndex, int count);

    bool Remove(T item);
}

而非Generic的ICollection定义以下:ui

public interface ICollection : IEnumerable
{   
    void CopyTo(Array array, int index);    
  
    int Count { get; }

    Object SyncRoot { get; }        
   
    bool IsSynchronized { get; }
}

与ICollection<T>相比较,ICollection实现了计算集合元素数目的功能,但明没有提供更改集合的功能。此外,ICollection还提供了同步的功能。而ICollection<T>则取消了同步的功能,这是由于对于Generic的集合,它们自己是线程安全的。this

这两个接口既简单又容易实现。假如要实现一个只读的ICollection<T>,那么在Add,Remove,和Clear方法中抛出异常便可。spa

这些接口一般与任何IList或IDictionary接口中的任意一个一块儿实现。线程

 

IList<T>和IList

若是想经过位置获取集合元素,那么IList<T>就是此类集合的标准接口。此外,因为IList<T>继承了ICollection<T>和IEnumerable<T>,因此此接口还提供了根据位置读取或写入元素,或者在指定的位置插入或删除元素。IList<T>定义以下设计

public interface IList<T> : ICollection<T>
{
    T this[int index] { get; set; }
    
    int IndexOf(T item);

    void Insert(int index, T item);    
  
    void RemoveAt(int index);
}

IndexOf方法在集合上执行线性搜索,若是没有发现指定元素,那么返回-1。3d

List类的IndexOf方法的实现:(List.IndexOf(T item)调用Array.IndexOf(_items, item, index, _size - index),而后调用EqualityComparer<T>.Default.IndexOf(array, value, startIndex, count))

internal virtual int IndexOf(T[] array, T value, int startIndex, int count) {
    int endIndex = startIndex + count;
    for (int i = startIndex; i < endIndex; i++) {
        if (Equals(array[i], value)) return i;
    }
    return -1;
}

而非Generic的IList则包含了更多成员,由于它继承ICollection

public interface IList : ICollection
{
    Object this[int index] {  get;set; }    
    int Add(Object value);
    bool Contains(Object value);
    void Clear();
    bool IsReadOnly  { get; }
    bool IsFixedSize  { get; }
    int IndexOf(Object value);
    void Insert(int index, Object value);
    void Remove(Object value);
    void RemoveAt(int index);
}

IList接口的Add方法返回一个整数,这是加到集合中元素的位置。而IList<T>接口的Add方法返回值为空。

C#中,List<T>就是典型的既实现了IList<T>又实现了IList的类。

public class List<T> : IList<T>, System.Collections.IList, IReadOnlyList<T>

C#数组也实现了generic和非generic的IList接口。C#中Array类的部分代码(实现IList接口的代码)

public abstract class Array : ICloneable, IList, IStructuralComparable, IStructuralEquatable
{
    ......
    public bool IsReadOnly
    { get { return false; } }

    public bool IsFixedSize
    {
        get { return true; }
    }

    Object IList.this[int index]
    {
        get { return GetValue(index); }
        set { SetValue(value, index); }
    }

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

    bool IList.Contains(Object value)
    {
        return Array.IndexOf(this, value) >= this.GetLowerBound(0);
    }

    void IList.Clear()
    {
        Array.Clear(this, this.GetLowerBound(0), this.Length);
    }

    int IList.IndexOf(Object value)
    {
        return Array.IndexOf(this, value);
    }

    void IList.Insert(int index, Object value)
    {
        throw new NotSupportedException(Environment.GetResourceString("NotSupported_FixedSizeCollection"));
    }

    void IList.Remove(Object value)
    {
        throw new NotSupportedException(Environment.GetResourceString("NotSupported_FixedSizeCollection"));
    }

    void IList.RemoveAt(int index)
    {
        throw new NotSupportedException(Environment.GetResourceString("NotSupported_FixedSizeCollection"));
    }
    ......
}

而Generic的IList<T>是在sealed class SZArrayHelper中实现的。SZ(Single dimensional, Zero-based)。请注意,若是你在技术与上调用add或者remove方法,那么会返回NotSupportedException异常

image

 

IReadonlyList<T>

为了与Windows运行时的制度集合互操做,Framework4.5引入了一个新的集合接口IReadOnlyList<T>。该接口自身就很是有用,也能够看做IList的缩减版,对外只公开用于只读的操做。其定义以下:

public interface IReadOnlyCollection<out T> : IEnumerable<T>
{
    int Count { get; }
}

public interface IReadOnlyList<out T> : IReadOnlyCollection<T>
{
    T this[int index] { get; }
}

由于类型参数仅仅用于输出位置,因此其标记为协变(covariant)。好比,一个cats列表,能够看做一个animals的只读列表。相反,在IList<T>中,T没有标记为协变,由于T应用于输入和输出位置。

你可能认为IList<T>派生自IReadonlyList<T>,而后,微软并无这么作,这是由于这么作就要求把IList<T>的成员移动到IReadonlyList<T>,这就给CLR4.5带来重的变化(程序员须要从新编辑程序以免运行时错误)。实际上,微软在IList<T>的实现类中手动地添加了对IReadonlyList<T>接口的实现。

在Windows运行时中IVectorView<T>与.NET Framework的IReadonlyList<T>相对应。

 

 

参考

线性搜索(Linear Search): http://en.wikipedia.org/wiki/Linear_search; http://blog.teamleadnet.com/2012/02/quicksort-binary-search-and-linear.html

数组实现IList<T>: http://stackoverflow.com/questions/11163297/how-do-arrays-in-c-sharp-partially-implement-ilistt/11164210#11164210

数组的奥秘: http://stackoverflow.com/questions/19914523/mystery-behind-system-array

协变: http://stackoverflow.com/questions/2719954/understanding-covariant-and-contravariant-interfaces-in-c-sharp