如何选择使用IEnumerable, ICollection, IList
来源:互联网 发布:java 字符串 substring 编辑:程序博客网 时间:2024/06/05 22:58
IEnumerable, ICollection, IList,每种接口只适合某些特定场景,如何区别使用呢?
IEnumerable接口,只提供了一个获取迭代器的方法,这也是为什么可以使用foreach遍历实现了IEnumerable接口集合的原因。
public interface IEnumerable{IEnumerator GetEnumerator();}
ICollection实现了IEnumerable接口,因此,除了拥有IEnumerable接口的能力,还包括其它能力。
public interface ICollection : IEnumerable{int Count{get;}bool IsSynchronized{get;}Object SyncRoot{get;}IEnumerator GetEnumerator();void CopyTo(Array array, int index);}
IList同时实现了ICollection和IEnumerable接口,在2个接口的基础上,可以添加、移除或清空集合,还提供了根据索引访问集合元素。
public interface IList : ICollection, IEnumerable{bool IsFixedSize{get;}bool IsReadOnly{get;}Object this[int index] {get;set;}int Add(Object value);void Clear();bool Contains(Object value);int IndexOf(Object value);void Insert(int index, Object value);void Remove(Object value);void RemoveAt(int index);}
总结:
● 如果只想遍历集合,使用IEnumerable, IEnumerable<T>
● 如果想遍历、修改集合,以及需要延迟加载的导航属性,使用ICollection, ICollection<T>
● 如果想遍历、修改、添加、清空、使用索引,使用IList, IList<T>
0 0
- 如何选择使用IEnumerable, ICollection, IList
- IEnumerable,ICollection ,IList ,ArrayList
- IList,ICollection,IEnumerable辨析
- IList,ICollection,IEnumerable,IListIList,ICollection,IEnumerable辨析
- IList,ICollection,IEnumerable,IListIList,ICollection,IEnumerable辨析
- IList、ICollection、IEnumerable 之辨析
- IList,ICollection,IEnumerable,IEnumerator,IQueryable
- IEnumerable、ICollection和IList接口
- IList,ICollection,IEnumerable,IEnumerator,IQueryable
- public abstract class CollectionBase:IList,ICollection,IEnumerable
- (原创)(C#随笔)IEnumerable< ICollection < IList区别
- C#中的 IList, ICollection ,IEnumerable 和 IEnumerator
- IList,ICollection,IEnumerable,IEnumerator,IQueryable详解
- IEnumerable,ICollection,IList,List之间的区别
- IEnumerable/ICollection/IList/List继承关系
- IEnumerable,ICollection,IList,List之间的区别
- IEnumerable,ICollection,IList,List之间的区别
- 阐述集合类的接口(IEnumerable,IEnumerator,ICollection,IList,IDictionary)
- 16.对子集padding 对父级没有影响
- js替换字符串中所有/全部空格
- Android中的基础----各种LaunchMode的使用
- 【操作系统】输入输出系统(下下)-思维导图0.0
- ScrollView使用技巧之实现滑动遮盖ImageView(View)
- 如何选择使用IEnumerable, ICollection, IList
- Android的Audio子系统
- 嵌入式开发第11天 (linux-c 环境搭建,命令)
- 【ANSI转UTF-8】用iconv命令解决Mac/Linux下编码问题
- tomcat配置文件server.xml详解
- Hibernate中,One2Many和Many2One的双向映射关系的XML实现方式
- 三维建模中shape of silhouette 的简要理解
- vijos 1104
- iOS/Android JSON封包与解包