ICollection 接口是 System.Collections 命名空间中类的基接口,ICollection 接口扩展 IEnumerable,IDictionary 和 IList 则是扩展 ICollection 的更为专用的接口。如果 IDictionary 接口和 IList 接口都不能满足所需集合的要求,则从 ICollection 接口派生新集合类以提高灵活性。

ICollection是IEnumerable的加强型接口,它继承自IEnumerable接口,提供了同步处理、赋值及返回内含元素数目的功能

 

一、ICollection接口的原型

 
C# 代码   复制
C#中ICollection介绍

C#中ICollection介绍namespace System.Collections
{
C#中ICollection介绍// 摘要:
C#中ICollection介绍//     定义所有非泛型集合的大小、枚举器和同步方法。
C#中ICollection介绍    [ComVisible(true)]
C#中ICollection介绍public interface ICollection : IEnumerable
{
C#中ICollection介绍// 摘要:
C#中ICollection介绍//     获取 System.Collections.ICollection 中包含的元素数。
C#中ICollection介绍//
C#中ICollection介绍// 返回结果:
C#中ICollection介绍//     System.Collections.ICollection 中包含的元素数。
C#中ICollection介绍//
C#中ICollection介绍// 摘要:
C#中ICollection介绍//     获取一个值,该值指示是否同步对 System.Collections.ICollection 的访问(线程安全)。
C#中ICollection介绍//
C#中ICollection介绍// 返回结果:
C#中ICollection介绍//     如果对 System.Collections.ICollection 的访问是同步的(线程安全),则为 true;否则为 false。
C#中ICollection介绍//
C#中ICollection介绍// 摘要:
C#中ICollection介绍//     获取一个可用于同步对 System.Collections.ICollection 的访问的对象。
C#中ICollection介绍//
C#中ICollection介绍// 返回结果:
C#中ICollection介绍//     可用于同步对 System.Collections.ICollection 的访问的对象。
C#中ICollection介绍C#中ICollection介绍// 摘要:
C#中ICollection介绍//     从特定的 System.Array 索引处开始,将 System.Collections.ICollection 的元素复制到一个 System.Array
C#中ICollection介绍//     中。
C#中ICollection介绍//
C#中ICollection介绍// 参数:
C#中ICollection介绍//   array:
C#中ICollection介绍//     作为从 System.Collections.ICollection 复制的元素的目标位置的一维 System.Array。System.Array
C#中ICollection介绍//     必须具有从零开始的索引。
C#中ICollection介绍//
C#中ICollection介绍//   index:
C#中ICollection介绍//     array 中从零开始的索引,将在此处开始复制。
C#中ICollection介绍//
C#中ICollection介绍// 异常:
C#中ICollection介绍//   System.ArgumentNullException:
C#中ICollection介绍//     array 为 null。
C#中ICollection介绍//
C#中ICollection介绍//   System.ArgumentOutOfRangeException:
C#中ICollection介绍//     index 小于零。
C#中ICollection介绍//
C#中ICollection介绍//   System.ArgumentException:
C#中ICollection介绍//     array 是多维的。- 或 -源 System.Collections.ICollection 中的元素数目大于从 index 到目标 array
C#中ICollection介绍//     末尾之间的可用空间。
C#中ICollection介绍//
C#中ICollection介绍//   System.ArgumentException:
C#中ICollection介绍//     源 System.Collections.ICollection 的类型无法自动转换为目标 array 的类型。
C#中ICollection介绍        void CopyTo(Array array, int index);
C#中ICollection介绍    }
C#中ICollection介绍}
C#中ICollection介绍

 

二、IEnumerable接口

 

1、IEnumerable接口是ICollection的父接口,凡实现此接口的类,都具备“可迭代”的能力。

2、IEnumerable接口只定义了一个方法:GetEnumerator,该方法将返回一个“迭代子”对象(或称为迭代器对象),是一个实现了IEnumerator接口的对象实例。

3、凡是实现了IEnumerable接口的类,都可以使用foreach循环迭代遍历。

 

 

三、简单的ICollection实现范例

 

C# 代码   复制
C#中ICollection介绍

C#中ICollection介绍public class MyCollectioin:ICollection
{
C#中ICollection介绍private string[] list;
C#中ICollection介绍private object root;
C#中ICollection介绍C#中ICollection介绍public MyCollection()
{
;
C#中ICollection介绍   }
C#中ICollection介绍C#中ICollection介绍public bool IsSynchronized
{
{
C#中ICollection介绍return true;
C#中ICollection介绍        }
C#中ICollection介绍    }
C#中ICollection介绍C#中ICollection介绍public int Count
{
C#中ICollection介绍get
{
C#中ICollection介绍return list.Length;
C#中ICollection介绍         }
C#中ICollection介绍     }
C#中ICollection介绍C#中ICollection介绍public void CopyTo(Array array,int index)
{
C#中ICollection介绍         list.CopyTo(array,index);
C#中ICollection介绍     }
C#中ICollection介绍C#中ICollection介绍public object SyncRoot
{
C#中ICollection介绍get
{
C#中ICollection介绍return root;
C#中ICollection介绍         }
C#中ICollection介绍    }
C#中ICollection介绍    #endregioin 
C#中ICollection介绍C#中ICollection介绍public IEnumerable GetEnumerator()
{
C#中ICollection介绍return list.GetEnumerator();
C#中ICollection介绍     }
C#中ICollection介绍C#中ICollection介绍#endregion
C#中ICollection介绍}
C#中ICollection介绍

 

 

四、ICollection<T>

 

ICollection<T>是可以统计集合中对象的标准接口。该接口可以确定集合的大小(Count),集合是否包含某个元素(Contains),复制集合到另外一个数组(ToArray),集合是否是只读的(IsReadOnly)。如果一个集合是可编辑的,那么可以调用Add,Remove和Clear方法操作集合中的元素。因为该接口继承IEnumerable<T>,所以可以使用foreach语句遍历集合。

 

ICollection<T>定义源码

 
C# 代码   复制
C#中ICollection介绍

C#中ICollection介绍public interface ICollection<T> : IEnumerable<T>
{
C#中ICollection介绍// Number of items in the collections.        
C#中ICollection介绍C#中ICollection介绍C#中ICollection介绍void Add(T item);
C#中ICollection介绍C#中ICollection介绍void Clear();
C#中ICollection介绍C#中ICollection介绍bool Contains(T item); 
C#中ICollection介绍C#中ICollection介绍// CopyTo copies a collection into an Array, starting at a particular
C#中ICollection介绍// index into the array.
C#中ICollection介绍// 
C#中ICollection介绍    void CopyTo(T[] array, int arrayIndex);
C#中ICollection介绍C#中ICollection介绍//void CopyTo(int sourceIndex, T[] destinationArray, int destinationIndex, int count);
C#中ICollection介绍
C#中ICollection介绍bool Remove(T item);
C#中ICollection介绍}
C#中ICollection介绍

相关文章: