IEnumerable、ICollection、IList、List关系和区别

IEnumerable、ICollection、IList、List之间的区别,本文分别分析了它的实现源码,从而总结出了它们之间的关系和不同之处。

首先我看看 IEnumerable:

 1 // 摘要:
 2   //   公开枚举器,该枚举器支持在指定类型的集合上进行简单迭代。
 3   //
 4   // 类型参数:
 5   //  T:
 6   //   要枚举的对象的类型。
 7   [TypeDependency("System.SZArrayHelper")]
 8   public interface IEnumerable<out T> : IEnumerable
 9   {
10     // 摘要:
11     //   返回一个循环访问集合的枚举器。
12     //
13     // 返回结果:
14     //   可用于循环访问集合的 System.Collections.Generic.IEnumerator<T>。
15     IEnumerator<T> GetEnumerator();
16   }

 IEnumerable<T> 实现IEnumerable接口方法,那IEnumberable做什么的,其实就提高可以循环访问的集合。说白了就是一个迭代。

再来看看ICollection:

1 // 摘要:
2  //   定义操作泛型集合的方法。
3  //
4  // 类型参数:
5  //  T:
6  //   集合中元素的类型。
7  [TypeDependency("System.SZArrayHelper")]
8  public interface ICollection<T> : IEnumerable<T>, IEnumerable

 原来ICollection<T> 同时继承IEnumerable<T>和IEnumerable两个接口,按我的理解就是,ICollection继续它们2个接口而且扩展了方法,功能强多了。

我们继续看IList:

1 public interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable
 

 IList 继承它们三个接口,怪不得功能这么多啊

最后来看看List:

1 public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable

 它们都是接口,只有List 是类,不仅实现它们的接口,而且还扩展了太多的方法给我利用,几乎所有功能都能实现了。

按照功能排序:List<T> 《IList<T> 《ICollection<T>《IEnumerable<T>

按照性能排序:IEnumerable<T>《ICollection<T>《IList<T>《List<T>

另一种解释:

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

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

一、ICollection接口的原型

 1 namespace System.Collections
 2 {
 3     // 摘要:
 4     //     定义所有非泛型集合的大小、枚举器和同步方法。
 5     [ComVisible(true)]
 6     public interface ICollection : IEnumerable
 7     {
 8         // 摘要:
 9         //     获取 System.Collections.ICollection 中包含的元素数。
10         //
11         // 返回结果:
12         //     System.Collections.ICollection 中包含的元素数。
13         int Count { get; }
14         //
15         // 摘要:
16         //     获取一个值,该值指示是否同步对 System.Collections.ICollection 的访问(线程安全)。
17         //
18         // 返回结果:
19         //     如果对 System.Collections.ICollection 的访问是同步的(线程安全),则为 true;否则为 false。
20         bool IsSynchronized { get; }
21         //
22         // 摘要:
23         //     获取一个可用于同步对 System.Collections.ICollection 的访问的对象。
24         //
25         // 返回结果:
26         //     可用于同步对 System.Collections.ICollection 的访问的对象。
27         object SyncRoot { get; }
28  
29         // 摘要:
30         //     从特定的 System.Array 索引处开始,将 System.Collections.ICollection 的元素复制到一个 System.Array
31         //     中。
32         //
33         // 参数:
34         //   array:
35         //     作为从 System.Collections.ICollection 复制的元素的目标位置的一维 System.Array。System.Array
36         //     必须具有从零开始的索引。
37         //
38         //   index:
39         //     array 中从零开始的索引,将在此处开始复制。
40         //
41         // 异常:
42         //   System.ArgumentNullException:
43         //     array 为 null。
44         //
45         //   System.ArgumentOutOfRangeException:
46         //     index 小于零。
47         //
48         //   System.ArgumentException:
49         //     array 是多维的。- 或 -源 System.Collections.ICollection 中的元素数目大于从 index 到目标 array
50         //     末尾之间的可用空间。
51         //
52         //   System.ArgumentException:
53         //     源 System.Collections.ICollection 的类型无法自动转换为目标 array 的类型。
54         void CopyTo(Array array, int index);
55     }
56 }

二、IEnumerable接口

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

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

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

三、简单的ICollection实现范例

 1 public class MyCollectioin:ICollection
 2 {
 3    private string[] list;
 4    private object root;
 5     
 6    public MyCollection()
 7    {
 8        list = new string[3]{"1","3","4"};
 9    }
10  
11     #region ICollection Members
12     public bool IsSynchronized
13     {
14         get{
15            return true;
16         }
17     }
18  
19      public int Count
20      {
21          get
22          {
23             return list.Length;
24          }
25      }
26  
27      public void CopyTo(Array array,int index)
28      {
29          list.CopyTo(array,index);
30      }
31      
32     public object SyncRoot
33     {
34         get
35          {
36             return root;
37          }
38     }
39     #endregioin
40      
41     #region IEnumerable Members
42      public IEnumerable GetEnumerator()
43      {
44         return list.GetEnumerator();
45      }
46  
47     #endregion
48 }

 四、ICollection<T>

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

ICollection<T>定义源码

 
 1 public interface ICollection<T> : IEnumerable<T>
 2 {
 3     // Number of items in the collections.       
 4     int Count { get; }
 5  
 6     bool IsReadOnly { get; }
 7  
 8     void Add(T item);
 9  
10     void Clear();
11  
12     bool Contains(T item);
13              
14     // CopyTo copies a collection into an Array, starting at a particular
15     // index into the array.
16     //
17     void CopyTo(T[] array, int arrayIndex);
18              
19     //void CopyTo(int sourceIndex, T[] destinationArray, int destinationIndex, int count);
20  
21     bool Remove(T item);
22 }

本文转自:https://www.cnblogs.com/sunliyuan/p/5816666.html

学习Mark

 
原文地址:https://www.cnblogs.com/YourDirection/p/12171529.html