在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到现在为止对这两个接口还是不太理解,不理解但是自己总是想着试着要搞明白,毕竟自己用的少,所以在此先记录一下.以备自己日后可以来翻查,同时也希望园子里 ...
随机推荐
- 无法找到AdbWinApi.dll问题解决 .
无法找到AdbWinApi.dll问题解决: 1. 现象: 在运行程序时,显示“无法启动此程序,因为丢失AdbWinApi.dll”.2. 解决方法: 到SDK的platform-tools和 ...
- Google, FaceBook, Amazon 加州求职记 (转)
http://blog.csdn.net/ithomer/article/details/8774006 http://www.myvisajobs.com 一年多前,出于显而易见的原因,下定决心肉身 ...
- Android 框架修炼-自己开发高效异步图片加载框架
一.概述 目前为止,第三方的图片加载框架挺多的,比如UIL , Volley Imageloader等等.但是最好能知道实现原理,所以下面就来看看设计并开发一个加载网络.本地的图片框架. 总所周知,图 ...
- Java基础知识强化之网络编程笔记16:Android网络通信之 使用Http的Get方式读取网络数据(基于HTTP通信技术)
使用Http的Get方式读取网络数据,使用Get方式与网络通信是最常见的Http通信,建立链接之后就可以通过输入流读取网络数据. 详见:Android(java)学习笔记209:采用get请求提交数据 ...
- Swift基础语法-内存管理, 自动引用计数
1. 工作机制 Swift和OC一样,采用自动引用计数来管理内存 当有一个强引用指向某一个对象时,该对象的引用计数会自动+1 当该强引用消失时,引用计数会自动-1 当引用计数为0时,该对象会被销毁 2 ...
- TextView设置动态改变颜色
通过TextView的setTextColor方法进行文本颜色的设置, 这里可以有3种方式进行设置: 第1种:tv.setTextColor(android.graphics.Color.RED);/ ...
- 深入理解计算机系统第二版习题解答CSAPP 2.15
只使用位级运算和逻辑运算,编写一个C表达式,它等价于x==y.换句话说,当x和y相等时它将返回1,否则就返回0. !(x ^ y)
- IP地址子网掩码、主机数、子网掩码及主机段的算法
http://wenku.baidu.com/view/2aa76cc6aa00b52acfc7ca6f.html很容易理解.
- MSP430常见问题之开发工具类
Q1:我自己做了一块MSP430F149的试验板,以前用下载线进行调试没有出现过问题,但是,最近我每次make后用下载线调试时,总是弹出一个窗口,给我提示:Could not find target ...
- maven打包技巧
http://www.infoq.com/cn/news/2011/06/xxb-maven-9-package/ "打包"这个词听起来比较土,比较正式的说法应该是"构建 ...