C#中ICollection介绍
类别:编程学习 浏览量:14986
时间:2014-9-10 C#中ICollection介绍
C#中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"};
}
ICollection Members
四、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);
}
标签:List
您可能感兴趣
- list使用linq排序
- C#泛型List的用法
- 织梦自定义表单怎么设置数据(让织梦artlist标签增加notypeid属性实现栏目过滤)
- 数组、ArrayList和List的区别
- python redis锁的使用(Python操作redis实例小结String、Hash、List、Set等)
- ubuntu中source指令(怎么修改Ubuntu的源列表source list详解)
- dedecms表单格式(DedeCMS arclist的分页使用教程)
- dedecms自定义字段(详解怎么样让DEDECMS的list标签支持weight排序的方法)
- MVC扩展HtmlHelper实现CheckBoxList
- python里list操作大全(Python列表List知识点总结)
- dedecms投稿页面(dedecms artlist读取全站最新文章的代码)
- python 自定义获取文件目录(Python使用os.listdir和os.walk获取文件路径与文件下所有目录的方法)
- dedecms二次开发教程(dedecms二次开发时使用{dede:arclist},{dede:list}获取附加表字段内容)
- dedecms标签分类(dedecms list增加noflag属性的方法 实现让列表标签不调用有推荐属性的文章)
- python list基本操作(Python3中列表list合并的四种方法)
- dede标签使用(解决{dede:arclist keyword='动态获取关键词'})
- 新款起亚霸锐实拍,比普拉多气派,配3.0T V6,引入国内或许能大卖(新款起亚霸锐实拍)
- ()
- 张勇 阿里新任掌门人(阿里新任掌门人)
- 毛戈平全国第一柜花落银泰 高端国货迎来 高光 时刻(毛戈平全国第一柜花落银泰)
- 14岁丧父 20岁丧母,从苦难走向辉煌的银泰创始人沈国军(14岁丧父20岁丧母从苦难走向辉煌的银泰创始人沈国军)
- 银泰集团董事长沈国军获评 北京影响力 十大企业家(银泰集团董事长沈国军获评)
热门推荐
- vue过滤器使用思路(vue 过滤器和自定义指令的使用)
- python和java的共同语法(Python和Java的语法对比分析语法简洁上python的确完美胜出)
- 如何来确定云服务器ecs的操作系统(远程登录Windows系统的ECS实例提示“远程桌面连接已断开”错误的解决方法)
- pycharm怎么创建django项目(Python、 Pycharm、Django安装详细教程图文)
- .net面试智力题
- Sql Server事件探查器的作用
- 手机网站自适应的方法
- jQuery商品属性选择的实现
- ecmascript编程语言(一篇文章弄懂ECMAScript中的操作符)
- 云服务器9兆带宽的上传速度是多少(云服务器网速多少怎么计算?云服务器的1兆带宽是多少的?)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9