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);
}
C#中IEnumerable、ICollection、IList、List之间的区别的更多相关文章
- 【转载】我也说 IEnumerable,ICollection,IList,List之间的区别
做C#的同学们,都知道,一类只能有一个继承类,但可以实现多个接口.这句话就告诉我们:IEnumerable,ICollection,IList,List区别了 首先我看看 IEnumerable: / ...
- IEnumerable,ICollection,IList,List之间的区别
做C#的同学们,都知道,一类只能有一个继承类,但可以实现多个接口.这句话就告诉我们:IEnumerable,ICollection,IList,List区别了 // 摘要: // 公开枚举器,该枚举器 ...
- C#中 IEnumerable, ICollection, IList, List的使用
List是類,實現了IList接口,IList繼承了ICollection,ICollection繼承了IEnumerable,IEnumerable是其中最底層的接口. 實現IEnumerable接 ...
- 服务器文档下载zip格式 SQL Server SQL分页查询 C#过滤html标签 EF 延时加载与死锁 在JS方法中返回多个值的三种方法(转载) IEnumerable,ICollection,IList接口问题 不吹不擂,你想要的Python面试都在这里了【315+道题】 基于mvc三层架构和ajax技术实现最简单的文件上传 事件管理
服务器文档下载zip格式 刚好这次项目中遇到了这个东西,就来弄一下,挺简单的,但是前台调用的时候弄错了,浪费了大半天的时间,本人也是菜鸟一枚.开始吧.(MVC的) @using Rattan.Co ...
- Asp.Net IEnumerable,ICollection,IList,List区别
做C#的同学们,都知道,一类只能有一个继承类,但可以实现多个接口.这句话就告诉我们:IEnumerable,ICollection,IList,List区别了 首先我看看 IEnumerable: / ...
- IEnumerable,ICollection,IList,List区别
做C#的同学们,都知道,一类只能有一个继承类,但可以实现多个接口.这句话就告诉我们:IEnumerable,ICollection,IList,List区别了 首先我看看 IEnumerable: / ...
- IEnumerable,ICollection,IList,List的使用
做C#的都知道:一类只能有一个继承类,但可以实现多个接口.这句话就告诉我们:IEnumerable,ICollection,IList,List区别了 首先我看看 IEnumerable: // ...
- IEnumerable<> ICollection <> IList<> 区别
IEnumerable< ICollection < IList区别 public interface IEnumerable { IEnumerator GetEnumerator(); ...
- 如何选择使用IEnumerable, ICollection, IList
IEnumerable, ICollection, IList,每种接口只适合某些特定场景,如何区别使用呢? IEnumerable接口,只提供了一个获取迭代器的方法,这也是为什么可以使用foreac ...
- Spring中Model,ModelMap以及ModelAndView之间的区别
原文链接:http://blog.csdn.net/zhangxing52077/article/details/75193948 Spring中Model,ModelMap以及ModelAndVie ...
随机推荐
- Best Cow Fences POJ - 2018 (二分)
Farmer John's farm consists of a long row of N (1 <= N <= 100,000)fields. Each field contains ...
- CodeForces 433C Ryouko's Memory Note (中位数定理)
<题目链接> 题目大意:给你一堆数字,允许你修改所有相同的数字成为别的数字,不过只能修改一次,问你修改后序列相邻数字的距离和最小是多少. 解题分析: 首先,修改不是任意的,否则那样情况太多 ...
- vue中的页面渲染方案
一.模板渲染 <div id="J_render_app"> <ul v-if="items.length"> <li v-for ...
- 多AG自动生成apk说明
-----------自动生成apk说明.txt 1017202130405658626378 自动实现思路几个变化的地方 就是 第一次放入文件10, 修改apktype 100130 10生成apk ...
- go defer笔记
1.函数中return xxx非原子指令 2.函数返回过程:先给返回值赋值:再调用defer:最后回到调用函数中 即:返回值 = xxx; defer; return; 3.多个defer调用顺序类似 ...
- LeetCode(119. 杨辉三角 II)
问题描述 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 3 输出: [1,3,3,1] 进阶: 你可以优化你的 ...
- [USACO08OPEN]寻宝之路Clear And Present Danger
OJ题号:洛谷2910 思路:Floyd #include<cstdio> #include<algorithm> using namespace std; int main( ...
- [计算机视觉]掩膜(mask)
转自->这里 刚开始涉及到图像处理的时候,在OpenCV等库中总会看到mask这么一个参数,非常的不理解,在查询一系列资料之后,写下它们,以供翻阅. 什么是掩膜(mask) 数字图像处理中的掩膜 ...
- iframe边距问题解决
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Dropping tests [POJ2976] [01分数规划]
Description 今年有 n 场 ACM-ICPC 竞赛,小明每场都有资格参加.第 i 场竞赛共有 b[i] 道题.小明预测第 i场他能做出 a[i] 道题.为了让自己看着更“大佬”一些,小明想 ...