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

IEnumerable接口是非常的简单,只包含一个抽象的方法GetEnumerator(),它返回一个可用于循环访问集合的IEnumerator对象。IEnumerator对象有什么呢?它是一个真正的集合访器,没有它,就不能使用foreach语句遍历集合或数组,因为只有IEnumerator对象才能访问集合中的项,假如连集合中的项都访问不了,那么进行集合的循环遍历是不可能的事情了。

1、IEnumerator接口

提供在普通集合中遍历的接口,有Current,MoveNext(),Reset(),其中Current返回的是object类型。

IEnumerator<T>:继承自IEnumerator,有Current属性,返回的是T类型。

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

2、IEnumerable接口

暴露一个IEnumerator,支持在普通集合中的遍历。

IEnumerable<T>:继承自IEnumerable,暴露一个IEnumerator<T>,支持在泛型集合中遍历。

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

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

3、ICollection接口

同时继承IEnumerable<T>和IEnumerable两个接口

     // 摘要:
// 定义操作泛型集合的方法。
//
// 类型参数:
// 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

实现IEnumerable接口

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace IEnumeratorSample
{
class Person : IEnumerable
{
public string Name;//定义Person的名字
public string Age;//定义Person的年龄 public Person(string name, string age)//为Person初始化(构造函数)
{
Name = name;
Age = age;
} private Person[] per; public Person(Person[] array)//重载构造函数,迭代对象
{
per = new Person[array.Length];//创建对象
for (int i = ; i < array.Length; i++)//遍历初始化对象
{
per[i] = array[i];//数组赋值
}
} public IEnumerator GetEnumerator()//实现接口
{
return new PersonEnum(per);
}
} class PersonEnum : IEnumerator//实现foreach语句内部,并派生
{
public Person[] _per;//实现数组
int position = -;//设置“指针” public PersonEnum(Person[] list)
{
_per = list;//实现list
} public bool MoveNext()//实现向前移动
{
position++;//位置增加
return (position < _per.Length);//返回布尔值
} public void Reset()//位置重置
{
position = -;//重置指针为-1
} public object Current//实现接口方法
{
get
{
try
{
return _per[position];//返回对象
}
catch (IndexOutOfRangeException)//捕获异常
{
throw new InvalidOperationException();//抛出异常信息
}
}
}
} class Program
{
static void Main(string[] args)
{
Person[] per = new Person[]
{
new Person("Jack",""),
new Person("David",""),
};
Person personlist = new Person(per);
//遍历对象
foreach (Person p in personlist)
{
Console.WriteLine("Name is " + p.Name + " and Age is " + p.Age);
}
Console.ReadKey();
}
}
}

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

相关博客1:http://blog.csdn.net/callmeback/article/details/8295648

相关博客2:http://www.cnblogs.com/shaosks/archive/2011/09/27/2193270.html

IEnumerable、IEnumerator、ICollection、IList、List的继承关系及简单使用的更多相关文章

  1. IEnumerable、ICollection、IList、List之间的区别与方法介绍

    区别 以下列出IEnumerable.ICollection.IList.List继承关系.(这里带有泛型,非泛型也是一样的关系) IEnumerable<T>: public inter ...

  2. C#中IEnumerable、ICollection、IList、List之间的区别

    IEnumerable.ICollection.IList.List之间的区别,本文分别分析了它的实现源码,从而总结出了它们之间的关系和不同之处. 首先我看看 IEnumerable: // 摘要: ...

  3. 【转载】我也说 IEnumerable,ICollection,IList,List之间的区别

    做C#的同学们,都知道,一类只能有一个继承类,但可以实现多个接口.这句话就告诉我们:IEnumerable,ICollection,IList,List区别了 首先我看看 IEnumerable: / ...

  4. Asp.Net IEnumerable,ICollection,IList,List区别

    做C#的同学们,都知道,一类只能有一个继承类,但可以实现多个接口.这句话就告诉我们:IEnumerable,ICollection,IList,List区别了 首先我看看 IEnumerable: / ...

  5. IEnumerable,ICollection,IList,List区别

    做C#的同学们,都知道,一类只能有一个继承类,但可以实现多个接口.这句话就告诉我们:IEnumerable,ICollection,IList,List区别了 首先我看看 IEnumerable: / ...

  6. IEnumerable,ICollection,IList,List之间的区别

    做C#的同学们,都知道,一类只能有一个继承类,但可以实现多个接口.这句话就告诉我们:IEnumerable,ICollection,IList,List区别了 // 摘要: // 公开枚举器,该枚举器 ...

  7. 服务器文档下载zip格式 SQL Server SQL分页查询 C#过滤html标签 EF 延时加载与死锁 在JS方法中返回多个值的三种方法(转载) IEnumerable,ICollection,IList接口问题 不吹不擂,你想要的Python面试都在这里了【315+道题】 基于mvc三层架构和ajax技术实现最简单的文件上传 事件管理

    服务器文档下载zip格式   刚好这次项目中遇到了这个东西,就来弄一下,挺简单的,但是前台调用的时候弄错了,浪费了大半天的时间,本人也是菜鸟一枚.开始吧.(MVC的) @using Rattan.Co ...

  8. IEnumerable,ICollection,IList,List的使用

    做C#的都知道:一类只能有一个继承类,但可以实现多个接口.这句话就告诉我们:IEnumerable,ICollection,IList,List区别了 首先我看看 IEnumerable:   // ...

  9. IEnumerable<> ICollection <> IList<> 区别

    IEnumerable< ICollection < IList区别 public interface IEnumerable { IEnumerator GetEnumerator(); ...

随机推荐

  1. Shell 基础教程

    一个比较好的shell基础教程: http://www.runoob.com/linux/linux-shell.html

  2. HDU - 1013

    wa了两遍: (1)没有弄清楚输入数据的范围,实际上是字符串输入,数字很大. (2)此题太水,没有标数据范围. #include<iostream> #include<cstdio& ...

  3. C#基础零碎知识点摘录

    1.类分为静态类个非静态类(实例类) 静态类不能创建对象,使用方法时,直接类名.方法名(),常用的静态类有Console类 实例类:创建对象时通过对象调用类的方法 2.当我们声明一个类成员为静态时,意 ...

  4. linux下 vi命令编辑/etc/my.cnf

    把my.cnf配置文件加个max_connections包括(插入命令,删除命令,修改命令.退出保存命令) 你要有这个文件写权限,shell下输入: vi /etc/my.cnf 进入vi后,按i移动 ...

  5. Diango 框架起步

    一.命令行搭建Django项目 安装django # 在指定解释器环境下安装django 1.11.9# 在真实python3环境下: pip3 install django==1.11.9# 在虚拟 ...

  6. Java实现大数乘法运算

    基本思路:将输入的两个大数以字符串的形式存储,然后转化成整型数组存储,通过整型数组进行乘法运算(采用分治的思想) 即乘法分配律,如AB*CD=AC(AD+BC)BD,将两个数组逐位相乘的结果对位存放在 ...

  7. P2866 [USACO06NOV]糟糕的一天Bad Hair Day--单调栈

    P2866 [USACO06NOV]糟糕的一天Bad Hair Day 题意翻译 农夫约翰有N (N \leq 80000)N(N≤80000)头奶牛正在过乱头发节.每一头牛都站在同一排面朝东方,而且 ...

  8. linux磁盘阵列 逻辑卷管理器

    Difficult doesn't mean impossible.It simply meansthat you have to work hard.困难并不代表不可能,它仅仅意味着你必须努力奋斗. ...

  9. Active information gathering-services enumeration

    1.Summaize the book  Practical  Web Penetration Testing first  nmap tools is important for gather in ...

  10. 网络流24题——分配问题 luogu 4014

    题目链接:这里 本题是一个典型的费用流问题,可以作为费用流建图模板使用 首先看到,每个人只能做一件工作,每件工作只能做一次,一个人做某件工作有一定的收益 那么我们建立一个超级源点st和超级终点ed,然 ...