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
您可能感兴趣
- pythonlist列表讲解(Python中将两个或多个list合成一个list的方法小结)
- list使用linq排序
- dedecms标签分类(dedecms list增加noflag属性的方法 实现让列表标签不调用有推荐属性的文章)
- dedecms表单格式(DedeCMS arclist的分页使用教程)
- python实现列表删除重复元素(Python代码实现删除一个list里面重复元素的方法)
- python list基本操作(Python3中列表list合并的四种方法)
- dedecms二次开发教程(dedecms二次开发时使用{dede:arclist},{dede:list}获取附加表字段内容)
- python把str转成list(python3 字符串/列表/元组str/list/tuple相互转换方法及join函数的使用)
- html元素和属性的区别(HTML5 HTMLCollection和NodeList的区别详解)
- C#中ArrayList与Array的区别
- C# Checklistbox的用法
- python 字典按key值排序(Python 按字典dict的键排序,并取出相应的键值放于list中的实例)
- MVC扩展HtmlHelper实现CheckBoxList
- mysql索引的比较规则(MySQL的Flush-List和脏页的落盘机制)
- ubuntu18.04更换软件源(ubuntu20.04 LTS系统默认源sources.list文件的修改)
- css按钮样式实例(CSS使用classList实现两个按钮样式的切换)
- 自制橡皮泥(自制橡皮泥)
- 还在卖 禁药西布曲明网上论斤卖(还在卖禁药西布曲明网上论斤卖)
- 微商在朋友圈热卖的 DL减肥咖啡 含违禁药物,你还敢买吗(微商在朋友圈热卖的)
- 八一节,说说中国女兵(八一节说说中国女兵)
- 王治郅菜鸟赛季已让八一带入正轨,大郅七大经典语录或是成功秘诀(王治郅菜鸟赛季已让八一带入正轨)
- 庆八一,重读经典红色语录,感悟互联网发展硬道理(重读经典红色语录)
热门推荐
- nginx为什么要配置https(Nginx配置Https安全认证的实现)
- ftp两种传输协议有什么区别(FTP与SFTP的区别 知多少)
- php把当前网页生成pdf(php转换上传word文件为PDF的方法基于COM组件)
- laravel前后端分离实现排序(laravel自定义分页的实现案例offset和limit)
- css42个实用技巧(你必须要知道的几个CSS技巧)
- CSS3 transition过渡效果
- python解析视频源码(基于python实现高速视频传输程序)
- vue的修饰符都有哪些(详解Vue的sync修饰符)
- angular 常用模块(详解Angular之路由基础)
- html5关闭页面代码(HTML5页面无缝闪开的问题及解决方案)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9