C#中IEnumerable、ICollection、IList、List之间的区别

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

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

⾸先我看看 IEnumerable:
// 摘要:
// 公开枚举器,该枚举器⽀持在指定类型的集合上进⾏简单迭代。

//
// 类型参数:
// T:
// 要枚举的对象的类型。

[TypeDependency("System.SZArrayHelper")]
public interface IEnumerable<out T> : IEnumerable
{
// 摘要:
// 返回⼀个循环访问集合的枚举器。

//
// 返回结果:
// 可⽤于循环访问集合的 System.Collections.Generic.IEnumerator<T>。

IEnumerator<T> GetEnumerator();
}
IEnumerable<T> 实现IEnumerable接⼝⽅法,那IEnumberable做什么的,其实就提⾼可以循环访问的集合。

说⽩了就是⼀个迭代。

再来看看ICollection:
// 摘要:
// 定义操作泛型集合的⽅法。

//
// 类型参数:
// T:
// 集合中元素的类型。

[TypeDependency("System.SZArrayHelper")]
public interface ICollection<T> : IEnumerable<T>, IEnumerable
原来ICollection<T> 同时继承IEnumerable<T>和IEnumerable两个接⼝,按我的理解就是,ICollection继续它们2个接⼝⽽且扩展了⽅法,功能强多了。

我们继续看IList:
public interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable
IList 继承它们三个接⼝,怪不得功能这么多啊
最后来看看List:
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接⼝的原型
namespace System.Collections
{
// 摘要:
// 定义所有⾮泛型集合的⼤⼩、枚举器和同步⽅法。

[ComVisible(true)]
public interface ICollection : IEnumerable
{
// 摘要:
// 获取 System.Collections.ICollection 中包含的元素数。

//
// 返回结果:
// System.Collections.ICollection 中包含的元素数。

int Count { get; }
//
// 摘要:
// 获取⼀个值,该值指⽰是否同步对 System.Collections.ICollection 的访问(线程安全)。

//
// 返回结果:
// 如果对 System.Collections.ICollection 的访问是同步的(线程安全),则为 true;否则为 false。

bool IsSynchronized { get; }
//
// 摘要:
// 获取⼀个可⽤于同步对 System.Collections.ICollection 的访问的对象。

//
// 返回结果:
// 可⽤于同步对 System.Collections.ICollection 的访问的对象。

object SyncRoot { get; }
// 摘要:
// 从特定的 System.Array 索引处开始,将 System.Collections.ICollection 的元素复制到⼀个 System.Array
// 中。

//
// 参数:
// array:
// 作为从 System.Collections.ICollection 复制的元素的⽬标位置的⼀维 System.Array。

System.Array
// 必须具有从零开始的索引。

//
// index:
// array 中从零开始的索引,将在此处开始复制。

//
// 异常:
// System.ArgumentNullException:
// array 为 null。

//
// System.ArgumentOutOfRangeException:
// index ⼩于零。

//
// System.ArgumentException:
// array 是多维的。

- 或 -源 System.Collections.ICollection 中的元素数⽬⼤于从 index 到⽬标 array
// 末尾之间的可⽤空间。

//
// System.ArgumentException:
// 源 System.Collections.ICollection 的类型⽆法⾃动转换为⽬标 array 的类型。

void CopyTo(Array array, int index);
}
}
⼆、IEnumerable接⼝
1、IEnumerable接⼝是ICollection的⽗接⼝,凡实现此接⼝的类,都具备“可迭代”的能⼒。

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

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

三、简单的ICollection实现范例
public class MyCollectioin:ICollection
{
private string[] list;
private object root;
public MyCollection()
{
list = new string[3]{"1","3","4"};
}
#region ICollection Members
public bool IsSynchronized
{
get{
return true;
}
}
public int Count
{
get
{
return list.Length;
}
}
public void CopyTo(Array array,int index)
{
list.CopyTo(array,index);
}
public object SyncRoot
{
get
{
return root;
}
}
#endregioin
#region IEnumerable Members
public IEnumerable GetEnumerator()
{
return list.GetEnumerator();
}
#endregion
}
四、ICollection<T>
ICollection<T>是可以统计集合中对象的标准接⼝。

该接⼝可以确定集合的⼤⼩(Count),集合是否包含某个元素(Contains),复制集合到另外⼀个数组(ToArray),集合是否是只读的(IsReadOnly)。

如果⼀个集合是可编辑的,那么可以调⽤Add,Remove和Clear⽅法操作集合中的元素。

因为该接⼝继承IEnumerable<T>,所以可以使⽤foreach语句遍历集合。

ICollection<T>定义源码
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);
}。

相关文档
最新文档