IList,ICollection,IEnumerable

祖宗: IEnumerable

此接口只有一个方法 GetEnumerator();

是FrameWork为了实现迭代器模式设计的接口。所有继承了IEnumerable的类,要使用foreach迭代器时,就需要使用该方法。因此也只有实现了该接口的类才可以使用foreach。

ICollection继承自IEnumerable,IList继承自ICollection

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

这两个接口都是为了给集合提供一些公用的方法。只是分了两个层次,IList比ICollection多几个方法,增加,移除成员。可以简单理解为:ICollection主要针对静态集合;IList主要针对动态集合

IListIList,ICollection,IEnumerable在命名空间System.Collections中。

IList<T>,ICollection<T>,IEnumerable<T>。在System.Collections.Generic 命名空间中。

IList<T>,ICollection<T>,IEnumerable<T>。是2.0引入泛型以后新增的。主要是提高重用性与类型安全。

IEnumerable<T>继承自IEnumerable

ICollection<T>继承自IEnumerable<T>

IList<T>继承自ICollection<T>

因此可以完全使用泛型接口,而放弃使用ICollection和IList。泛型接口提供了更好的类型安全和编译时的检验。

补充:

IEnumerable<T>和IEnumerable都只有一个方法。

ICollection<T>和ICollection的结构是不一样的。ICollection<T>比ICollection多几个方法。它包含了几个IList中的几个方法。也许是对以前的改进。

通俗理解:

IList接口可以使用更多的方法.比如你看一个集合是否包含相应实体, IEnumerable不行,而

IList里有Contains,相应的实现了IList的可以添加,删除相应实体.而IEnumerable不行.

但是这不是说IList就比IEnumerable好,就是因为IList实现的功能多.相对来说限制大了,你看

Object.任何类都可用作Object.这就是因为他简单.同理.能为IList表达的数据集.一定能为IEnumerable表达.而能为IEnumerable表达不一定能为IList表达.你可以想想Linq To Object里的方法为什么是对IEnumerable接口了而不是选择IList接口.

性能对于我们来说不是问题.就算是问题.你问的性能是什么.是IEnumerable得到Current快还是什么的.

那么我想说,这是接口,他本身没有实现,怎么比较性能.就算有.也是List<T>之类的类才会有的.

public interface IEnumerable

{

     IEnumerator GetEnumerator();

}

public interface IEnumerator

{

    bool MoveNext();

    object Current { get; }

    void Reset();

}

public interface IList : ICollection, IEnumerable

{

    // Methods     int Add(object value);

    void Clear();

    bool Contains(object value);

    int IndexOf(object value);

    void Insert(int index, object value);

    void Remove(object value);

    void RemoveAt(int index);

// Properties     bool IsFixedSize { get; }

    bool IsReadOnly { get; }

    object this[int index] { get; set; }

}

 
原文地址:https://www.cnblogs.com/yxyht/p/2874603.html