C#中IEnumerable、ICollection、IList、List之间的区别
作者:石shi 发布时间:2022-07-27 18:15:07
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>
{
// Numberof itemsin the collections.
int Count { get; }
bool IsReadOnly { get; }
voidAdd(T item);
void Clear();
boolContains(T item);
// CopyTo copies a collectioninto an Array, startingat 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);
}
来源:https://www.cnblogs.com/sunliyuan/p/5816666.html


猜你喜欢
- 一:软键盘自动弹出。private EditText top_middle;//输入框//-------------------------
- 概念引入我们都知道,Java 创建的对象都是被分配到堆内存上,但是事实并不是这么绝对,通过对Java对象分配的过程分析,可以知道有两个地方会
- 在项目中经常要用到将字符串解析成Locale,但是没有一个比较好用的类。java本身提供了3个构造函数,但是实际使用过程中,需要自己解析,比
- 最近由于项目需求,项目中需要实现一个WebSSH连接终端的功能,由于自己第一次做这类型功能,所以首先上了GitHub找了找有没有现成的轮子可
- 对于springboot应用,需要以下几个步骤springboot应用开启endpoint,添加actuator的以来和promethus的
- 本文实例为大家分享了java * 实现在线人数统计的具体代码,供大家参考,具体内容如下1.在工程中创建监听类SessionListener
- flutter中的布局flutter布局机制的核心是组件。在flutter中,几乎所有的东西都是组件,布局模型也不例外。图片,Icon, 文
- 背景:最近小组进行一个环境比较恶劣的项目,由于没有真实的测试环境,决定上云,最终选择国外的heroku,折腾半天,其中有一些坑在这里记录下来
- 在平时开发应用的时候,经常会遇到列表排序、滑动删除的需求。如果列表效果采用的是 ListView 的话,需要经过自定义 View 才能实现效
- 1.vs中生成dll对应的生成dll的cpp如下 #include<opencv2/opencv.hpp>#inclu
- java自定义切面增强写代码时会遇到一些有些重复机械的工作, 这个时候就可以运用切面技术来帮我们节约时间介绍如何使用自定义注解增强方法, 实
- 场景日常开发,if-else语句写的不少吧??当逻辑分支非常多的时候,if-else套了一层又一层,虽然业务功能倒是实现了,但是看起来是真的
- ①概念二叉搜索树又称二叉排序树,它或者是一棵空树**,或者是具有以下性质的二叉树:若它的左子树不为空,则左子树上所有节点的值都小于根节点的值
- 1.替换符的使用(1)在 app-android-defaultConfig (或者多渠道打包)下面可以这样使用android { &nbs
- engine的实现结构elasticsearch对于索引中的数据操作如读写get等接口都封装在engine中,同时engine还封装了索引的
- 本文总结了Java的关键字与标识符。分享给大家供大家参考,具体如下:相关内容:关键字:定义特点用于定义数据类型的关键字用于定义流程控制的关键
- 通过spring注解开发,测试单例和多例区别1.注解和配置两种用法形式配置版:注解版:2.在spring框架中,scope作用域默认是单例的
- 第一步:配置管理器中新建解决方案配置第二步:定义条件编译符号:第三步:在代码中使用自定义的条件编译#if CustomDebugConsol
- 本文实例为大家分享了Unity实现简单虚拟摇杆的具体代码,供大家参考,具体内容如下需求:点击创建一个虚拟摇杆底盘,鼠标拖拽时候上方摇杆会跟随
- java异常分为两大类,Checked异常和Runtime异常,Checked异常都是在编译阶段可以被处理的异常。Checked异常和Run