对于很多刚开始学习C#同学来说经常会遇到IEnumerable这个关键字,enumerate在字典里的解释是列举,枚举,因此可想而知这个关键字肯定是和列举数据有关的操作。

 public interface IEnumerable {       //IEnumerable只有一个方法,返回可循环访问集合的枚举数。
IEnumerator GetEnumerator()
;
} public interface IEnumerator { // 方法
//移到集合的下一个元素。如果成功则返回为 true;如果超过集合结尾,则返回false。
bool MoveNext();
// 将集合设置为初始位置,该位置位于集合中第一个元素之前
void Reset();
// 属性:获取集合中的当前元素
object Current { get; }
}

看我们手动自定义的Student类,以及Student1,StudentEnum类(分别继承了IEnumerable,IEnumerator接口),上代码

 using System;
using System.Collections;
using System.Text;
using System.Threading.Tasks; namespace Enumerate
{
public class Student
{ public string Name;
public int Score;
public Student(string name, int score)
{ this.Name = name;
this.Score = score;
}
} public class Student1: IEnumerable { public Student[] students;
public Student1(Student[] stArray)
{
students = new Student[stArray.Length];
for (int i = ; i < stArray.Length; i++)
{
students[i] = stArray[i];
}
}
public IEnumerator GetEnumerator()
{
//throw new NotImplementedException();
return new StudentEnum(students);
}
} public class StudentEnum : IEnumerator
{
public Student[] students; int position = -; public StudentEnum(Student[] studentlist)
{ students = studentlist;
} public object Current
{
//get { throw new NotImplementedException(); }
get { try
{
return students[position];
}
catch (Exception ex)
{ return ex.Message;
}
}
} public bool MoveNext()
{
//throw new NotImplementedException();
position++;
return position >= students.Length ? false : true;
} public void Reset()
{
//throw new NotImplementedException();
position = -;
}
} class Program
{
static void Main(string[] args)
{
Student[] studentArrary = new Student[] { new Student("test1",),
new Student("test2",),
};
Student1 studenlist = new Student1(studentArrary);
foreach (Student item in studenlist)
{ Console.WriteLine(item.Name +" "+ item.Score); }
Console.Read();
}
}
}

IEnumerable和IEnumerator有什么区别?

1、一个Collection要支持foreach方式的遍历,必须实现IEnumerable接口(亦即,必须以某种方式返回IEnumerator object)。
 
2、IEnumerator object具体实现了iterator(通过MoveNext(),Reset(),Current)。
 
3、从这两个接口的用词选择上,也可以看出其不同:IEnumerable是一个声明式的接口,声明实现该接口的class是“可枚举(enumerable)”的,但并没有说明如何实现枚举器(iterator);IEnumerator是一个实现式的接口,IEnumerator object就是一个iterator。
 
4、IEnumerable和IEnumerator通过IEnumerable的GetEnumerator()方法建立了连接,client可以通过IEnumerable的GetEnumerator()得到IEnumerator object,在这个意义上,将GetEnumerator()看作IEnumerator object的传递方法。

在C#中IEnumerable与IEnumerator的更多相关文章

  1. 关于迭代器中IEnumerable与IEnumerator的区别

    首先是IEnumerable与IEnumerator的定义: 1.IEnumerable接口允许使用foreach循环,包含GetEnumerator()方法,可以迭代集合中的项. 2.IEnumer ...

  2. C#中IEnumerable和IEnumerator区别

    IEnumerator:是一个真正的集合访问器,提供在普通集合中遍历的接口,有Current,MoveNext(),Reset(),其中Current返回的是object类型.IEnumerable: ...

  3. 细说 C# 中的 IEnumerable和IEnumerator接口

    我们先思考几个问题: 为什么在foreach中不能修改item的值? 要实现foreach需要满足什么条件? 为什么Linq to Object中要返回IEnumerable? 接下来,先开始我们的正 ...

  4. C#中的 IList, ICollection ,IEnumerable 和 IEnumerator

    IList, ICollection ,IEnumerable 很显然,这些都是集合接口的定义,先看看定义: // 摘要: // 表示可按照索引单独访问的对象的非泛型集合. [ComVisible(t ...

  5. 迭代器学习之一:使用IEnumerable和IEnumerator接口

    写博客是检验我学习的成果之一以及自我总结的一种方式,以后会经常利用这种方式进行技术交流和自我总结,其中认识不深难免会有错误,但是一直懂得不懂就问,不懂就学的道理! 1.首先看一个简单的列子 , , , ...

  6. C# ~ 从 IEnumerable / IEnumerator 到 IEnumerable<T> / IEnumerator<T> 到 yield

    IEnumerable / IEnumerator 首先,IEnumerable / IEnumerator 接口定义如下: public interface IEnumerable /// 可枚举接 ...

  7. IEnumerable和IEnumerator

    概述 IEnumerable和IEnumerator接口存在的意义:用来实现迭代的功能! public interface IEnumerable { IEnumerator GetEnumerato ...

  8. IEnumerable和IEnumerator 详解 (转)

    原文链接:http://blog.csdn.net/byondocean/article/details/6871881 参考链接:http://www.cnblogs.com/hsapphire/a ...

  9. C#基础知识系列九(对IEnumerable和IEnumerator接口的糊涂认识)

    前言 IEnumerable.IEnumerator到现在为止对这两个接口还是不太理解,不理解但是自己总是想着试着要搞明白,毕竟自己用的少,所以在此先记录一下.以备自己日后可以来翻查,同时也希望园子里 ...

随机推荐

  1. JavaScript,Java,php的区分大小写问题

    JavaScript 对大小写敏感. JavaScript 对大小写是敏感的.JavaScript属于弱类型语言 当编写 JavaScript 语句时,请留意是否关闭大小写切换键. 函数 getEle ...

  2. 命令行一键清除IE记录

    清除Internet临时文件 RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8 清除Cookies RunDll32.exe InetCpl.cpl, ...

  3. WinServer 之 发布WebService后调用出现" The test form is only available for requests from the local machine. "

    当您尝试从远程计算机访问 Web 服务时,不会显示“调用”按钮.并且,您会收到以下错误信息: The test form is only available for requests from the ...

  4. 配置LINUX为路由

    配置:关闭防火墙 linux1    地址1: 192.168.10.10/24 地址2:192.168.20.10/24(不指定网关,做为路由,自己就是网关) linux2    地址1: 192. ...

  5. SQL的几道题目

    1.构造数据插入方案表t_project_finish表 a)将addtime更新为当前时间的前一天 首先想到的是addtime=addtime-1,然后就开始验证这个想法. 插入一行数据,包括主键和 ...

  6. Java获取 JVM 运行信息

    import java.lang.management.ClassLoadingMXBean; import java.lang.management.ManagementFactory; impor ...

  7. Redis主备复制

    Redis 支持 Master-Slave(主从)模式,Redis Server 可以设置为另一个 Redis Server 的主机(从机),从机定期从主机拿数据.特殊的,一个从机同样可以设置为一个 ...

  8. [java小笔记] 关于数组内存管理的理解

    数组是大多数编程语言都提供的一种复合结构,如果程序需要多个类型相同的变量时,就可以考虑定义一个数组,java语言的数组变量时引用类型的变量,因此具有java引用变量的特性.在使用数组之前必须对数组对象 ...

  9. (转)战斗bug技巧全攻略

    原文地址:http://www.cnblogs.com/manuosex/p/3736077.html 程序员不是有一幅这样的对联吗 上联:一个项目两部电脑三餐盒饭只为四千工资搞得五脏俱损六神无主仍然 ...

  10. PHP基础1

    PHP:相当于客户端和MySQL之间的一堵墙 Apache(阿帕奇):是web服务器软件 localhost:相当于一个域名   一.wampserver http.conf:用来配置Apache p ...