在C#中IEnumerable与IEnumerator
对于很多刚开始学习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的更多相关文章
- 关于迭代器中IEnumerable与IEnumerator的区别
首先是IEnumerable与IEnumerator的定义: 1.IEnumerable接口允许使用foreach循环,包含GetEnumerator()方法,可以迭代集合中的项. 2.IEnumer ...
- C#中IEnumerable和IEnumerator区别
IEnumerator:是一个真正的集合访问器,提供在普通集合中遍历的接口,有Current,MoveNext(),Reset(),其中Current返回的是object类型.IEnumerable: ...
- 细说 C# 中的 IEnumerable和IEnumerator接口
我们先思考几个问题: 为什么在foreach中不能修改item的值? 要实现foreach需要满足什么条件? 为什么Linq to Object中要返回IEnumerable? 接下来,先开始我们的正 ...
- C#中的 IList, ICollection ,IEnumerable 和 IEnumerator
IList, ICollection ,IEnumerable 很显然,这些都是集合接口的定义,先看看定义: // 摘要: // 表示可按照索引单独访问的对象的非泛型集合. [ComVisible(t ...
- 迭代器学习之一:使用IEnumerable和IEnumerator接口
写博客是检验我学习的成果之一以及自我总结的一种方式,以后会经常利用这种方式进行技术交流和自我总结,其中认识不深难免会有错误,但是一直懂得不懂就问,不懂就学的道理! 1.首先看一个简单的列子 , , , ...
- C# ~ 从 IEnumerable / IEnumerator 到 IEnumerable<T> / IEnumerator<T> 到 yield
IEnumerable / IEnumerator 首先,IEnumerable / IEnumerator 接口定义如下: public interface IEnumerable /// 可枚举接 ...
- IEnumerable和IEnumerator
概述 IEnumerable和IEnumerator接口存在的意义:用来实现迭代的功能! public interface IEnumerable { IEnumerator GetEnumerato ...
- IEnumerable和IEnumerator 详解 (转)
原文链接:http://blog.csdn.net/byondocean/article/details/6871881 参考链接:http://www.cnblogs.com/hsapphire/a ...
- C#基础知识系列九(对IEnumerable和IEnumerator接口的糊涂认识)
前言 IEnumerable.IEnumerator到现在为止对这两个接口还是不太理解,不理解但是自己总是想着试着要搞明白,毕竟自己用的少,所以在此先记录一下.以备自己日后可以来翻查,同时也希望园子里 ...
随机推荐
- 命令行一键清除IE记录
清除Internet临时文件 RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8 清除Cookies RunDll32.exe InetCpl.cpl, ...
- python--列表的使用
1.定义列表: names = ['Mo',"Tenglan",'Eric'] 通过下标访问列表中的元素,下标从0开始计数 >>> names[0] 'Mo' & ...
- Android View的绘制机制流程深入详解(四)
本系列文章主要着重深入介绍Android View的绘制机制及流程,第四篇主要介绍Android自定义View及ViewGroup的实现方法和流程. 主要介绍了自绘控件.自定义组合控件.自定义继承控件 ...
- mapping 详解1(mapping type)
映射(mapping) 映射是定义一个文档以及其所包含的字段如何被存储和索引的方法. 例如,用映射来定义以下内容: 哪些 string 类型的 field 应当被当成当成 full-text 字段 哪 ...
- [转]ORACLE 异常错误处理
本文转自:http://www.cnblogs.com/soundcode/archive/2012/01/10/2318385.html 本篇主要内容如下: 5.1 异常处理概念 5.1.1 预定义 ...
- C#总结3
第四章:文件管理 File类: 对于File类,里面的方法都是静态方法,就是直接可以用FIle来“.”: 记几个方法吧:File.Copy(string filename1,string f ...
- 关于Eclispe插件开发起步(一)
Eclipse中三个最吸引人的地方是: 第一是它创新性的图形API,就是SWT/JFace. 第二是它的插件机制. 第三个是利用它的插件机制开发众多功能强大的插件. 插件成就了Eclipse的今天.E ...
- msp430f149的低功耗模式
430的低功耗确实很强啊,虽然和VR单片机比起来速度慢了好多.在CPU进行工作时,如果没有什么事情干,就得进入低功耗模式啦,LMPX(0~4)这几种模式的具体事项就是如下的,得记住了. 一,运行模式M ...
- Linux安装后的系统配置
第一步: Linux系统安装之后,可以设置系统的日期和时间.给系统添加用户.安装软件.在Red Hat网络中注册机器以及完成其他任务.设置代理将允许用户从一开始就配置环境,从 而使用户能够快速地开始使 ...
- Android Studio中文组(中文社区)
Android Studio中文组(中文社区)http://www.android-studio.org/