对于很多刚开始学习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. 解决Windows2008Server上PLSQL登录时报ORA-12557

    公司的Oracle服务端是安装在一台Linux服务器上,版本号为11.1.0.7.0.我们开发的系统部署在Windows 2008 Server(x64),因为偶尔需要调用Oracle数据库,所以最开 ...

  2. appscan 安全漏洞修复办法

    appscan 安全漏洞修复办法http://www.automationqa.com/forum.php?mod=viewthread&tid=3661&fromuid=21

  3. git 版本控制系统初学

    Git -The stupid content tracker, 傻瓜内容跟踪器,是一个由Linux内核开发者Linus为了更好地管理Linux内核开发而创立的分布式版本控制软件. 1.建立本地git ...

  4. 电脑小白学习软件开发-C#语言基础之循环重点讲解,习题

    写代码也要读书,爱全栈,更爱生活.每日更新原创IT编程技术及日常实用视频. 我们的目标是:玩得转服务器Web开发,搞得懂移动端,电脑客户端更是不在话下. 本教程是基础教程,适合任何有志于学习软件开发的 ...

  5. 结果集一组数据的第几条ROW_NUMBER基本用法

    因为项目中用到,今天来记录下 ROW_NUMBER的用法. 说明:返回结果集分区内行的序列号,每个分区的第一行从 1 开始. 语法:ROW_NUMBER () OVER ([ <partitio ...

  6. 【转载】Spark SQL之External DataSource外部数据源

    http://blog.csdn.net/oopsoom/article/details/42061077 一.Spark SQL External DataSource简介 随着Spark1.2的发 ...

  7. spark向量、矩阵类型

    先来个普通的数组: scala> var arr=Array(1.0,2,3,4) arr: Array[Double] = Array(1.0, 2.0, 3.0, 4.0) 可以将它转换成一 ...

  8. 第六章 jQuery操作表单

    1.单行文本框的应用 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:// ...

  9. asp下实现多条件模糊查询SQL语句

    常写一个简单的模糊查询的SQL语句格式可以如下例: sql="select * from 表名 where 字段名 like ’%" & request.form(&quo ...

  10. SQL 拼接多个字段的值&一个字段多条记录的拼接 [轉]

    例如student表: studentID studentName studentScore 01 Alice 90 02 Bill 95 03 Cindy 100 一.拼接多个字段的值 select ...