MVC学习笔记--IEnumerable的用法


IEnumerable的用法

 

IEnumerable和IEnumerable<T>接口在.NET中是非常重要的接口,它允许开发人员定义foreach语句功能的实现 并支持非泛型方法的简单的迭代,IEnumerable和IEnumerable<T>接口是.NET Framework中最基本的集合访问器。它定义了一组扩展方法,用来对数据集合中的元素进行遍历、过滤、排序、搜索等操作。

 

IEnumerable接口是非常的简单,只包含一个抽象的方法GetEnumerator(),它返回一个可用于循环访问集合的IEnumerator对象。

IEnumerator对象有什么呢?它是一个真正的集合访器,没有它,就不能使用foreach语句遍历集合或数组,因为只有IEnumerator对象才能访问集合中的项,假如连集合中的项都访问不了,那么进行集合的循环遍历是不可能的事情了。

 

一、IEnumerable、IEnumerator、ICollection、IList、List

 

IEnumerator:提供在普通集合中遍历的接口,有Current,MoveNext(),Reset(),其中Current返回的是object类型。
IEnumerable: 暴露一个IEnumerator,支持在普通集合中的遍历。


IEnumerator<T>:继承自IEnumerator,有Current属性,返回的是T类型。
IEnumerable<T>:继承自IEnumerable,暴露一个IEnumerator<T>,支持在泛型集合中遍历。

 

1、IEnumerable接口
 
C# 代码    复制

    // 摘要:  // 公开枚举器,该枚举器支持在指定类型的集合上进行简单迭代。  //  // 类型参数:  // T:  // 要枚举的对象的类型。  [TypeDependency("System.SZArrayHelper")]  public interface IEnumerable<out T> : IEnumerable  {  // 摘要:  // 返回一个循环访问集合的枚举器。  //  // 返回结果:  // 可用于循环访问集合的 System.Collections.Generic.IEnumerator<T>。  IEnumerator<T> GetEnumerator();  } 

 

可以看到,GetEnumerator方法返回对另一个接口System.Collections.IEnumerator的引用。这个接口提供了基础设施,调用方可以用来移动IEnumerable兼容容器包含的内部对象。

 

2、IEnumerator接口

 
C# 代码    复制

public interface IEnumerator  {   bool MoveNext(); //将游标的内部位置向前移动   object Current{get;} //获取当前的项(只读属性)   void Reset(); //将游标重置到第一个成员前面  } 

 

 3、ICollection接口:ICollection<T> 同时继承IEnumerable<T>和IEnumerable两个接口

 
C# 代码    复制

    // 摘要:  // 定义操作泛型集合的方法。  //  // 类型参数:  // T:  // 集合中元素的类型。  [TypeDependency("System.SZArrayHelper")]  public interface ICollection<T> : IEnumerable<T>, IEnumerable 

 

4、IList接口

public interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable

 

5、List的定义

public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable

 

原文链接:http://www.studyofnet.com/news/452.html


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM