C#中ICollection介绍
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#中ICollection介绍的更多相关文章
- [BIM]BIM中IDM介绍
参考:http://blog.fang.com/25866228/10613454/articledetail.htm IDM的全称是Information Delivery Manual,信息交付手 ...
- ios中框架介绍
ios中框架介绍 参考博客: 参考文章:框架介绍 框架介绍 框架就是一个目录,一个目录包含了共享库,访问共享库里面的代码的头文件,和其他的图片和声音的资源文件.一个共享库定义的方法和函数可以被应用程序 ...
- Burp Suite Intruder中爆破模式介绍
Burp Suite Intruder中爆破模式介绍 - Introduction to Burst Mode in Burp Suite Intruder 1.sniper模式 使用单一的Payl ...
- Django中ORM介绍和字段及字段参数 Object Relational Mapping(ORM)
Django中ORM介绍和字段及字段参数 Object Relational Mapping(ORM) ORM介绍 ORM概念 对象关系映射(Object Relational Mapping,简 ...
- [转]C# 互操作性入门系列(一):C#中互操作性介绍
传送门 C#互操作系列文章: C# 互操作性入门系列(一):C#中互操作性介绍 C# 互操作性入门系列(二):使用平台调用调用Win32 函数 C# 互操作性入门系列(三):平台调用中的数据封送处理 ...
- 6月20日 Django中ORM介绍和字段、字段参数、相关操作
一.Django中ORM介绍和字段及字段参数 二.Django ORM 常用字段和参数 三.Django ORM执行原生SQL.在Python脚本中调用Django环境.Django终端打印SQL语句 ...
- [BIM]BIM中IFD介绍
第三大支柱IFD - 确定交换的信息和你要的信息是同一个东西 IFD的全称是International Framework for Dictionaries,中文可以叫“国际字典框架”,和前两者IFC ...
- [BIM]BIM中IFC介绍
ifc是干什么的,看下图 ifc架构图 下文转自:http://www.bimcn.org/cjwt/201506053789.html IFC目前是国际通用的BIM标准,现在很多BIM软件都采用其作 ...
- hive中简单介绍分区表
所介绍内容基本上是翻译官方文档,比较肤浅,如有错误,请指正! hive中创建分区表没有什么复杂的分区类型(范围分区.列表分区.hash分区.混合分区等).分区列也不是表中的一个实际的字段,而是一个或者 ...
随机推荐
- python_getpass 模块的使用
可以实现输入用户密码的时候隐藏输入显示.更加安全. 默认自带Password: 的提示 如果自己指定提示内容就用自己的替换默认 import getpass passwd = getpass.getp ...
- linux 定时任务到秒级
linux crontab 只有到分钟级别的 有两种方法 方法1.写个sh脚本,循环(下例为每秒访问一次百度) #! /bin/bash PATH=/bin:/sbin:/usr/bin:/usr/l ...
- P3723 [AH2017/HNOI2017]礼物
题目链接:[AH2017/HNOI2017]礼物 题意: 两个环x, y 长度都为n k可取 0 ~ n - 1 c可取任意值 求 ∑ ( x[i] - y[(i + k) % n + 1] ...
- HAOI2018 简要题解
这套题是 dy, wearry 出的.学长好强啊,可惜都 \(wc\) 退役了.. 话说 wearry 真的是一个计数神仙..就没看到他计不出来的题...每次考他模拟赛总有一两道毒瘤计数TAT 上午的 ...
- THUSC2017题解
THUSC2017题解 题目都是在LOJ上交的. chocolate LOJ#2977巧克力 这题看着就让人想起了百度之星复赛的\(T5\),就是这题. 因为种类的个数很多,所以把每个种类随意\(ra ...
- python 去重
List: listA = ['python','python','言','是','一','门','动','态','语','言'] print sorted(set(listA), key = lis ...
- Linux下设置VSCode为默认的文本编辑器
解决方法 执行一下命令 xdg xdg-mime default code.desktop text/plain Debian alternatives system sudo update-alte ...
- ubuntu 百度云
下载链接:https://pan.baidu.com/s/1HBu5T3PZ8JsS93PgOKnUYw提取密码:6J1A 在终端中输入命令: sudo dpkg -i bcloud_3..1_all ...
- 第四篇 - 爬取前程无忧python相关工作
环境:python3 pycharm 模块:requests,xlwt,urllib.request,re 正常三步走: 1.获取源代码 2.匹配源代码,获得目标数据 3.存储到文件中 直接上代 ...
- C sockets Errno
在Windows下进行网络编程,免不了出现各种错误.在Linux下可以使用errno查看错误,但是根据stackoverflow上说,windows下应该使用: FormatMessage() WSA ...