IEnumerable与IEnumerator
IEnumerable接口
IEnumerable接口:实现该接口的类,表明该类下有可以枚举的元素
public interface IEnumerable
{
//返回一个实现了IEnumerator接口的对象
//我的理解:实现该接口的类,用其下哪个可枚举的元素实现该方法那么当该类被枚举时,将得到该类下的某个可枚举元素的元素
IEnumerator GetEnumerator();
}
public class TestEnumerable : IEnumerable
{
//可枚举元素
private Student[] _students; public TestEnumerable(Student[] list)
{
_students = list;
} public IEnumerator GetEnumerator()
{
//当TestEnumerator被枚举时,将得到_students的元素
return _students.GetEnumerator();
}
} public class Student
{
public string ID { get; set; } public string Name { get; set; } public Student(string id, string name)
{
ID = id;
Name = name;
}
}
调用代码:
public static void Main()
{
Student[] students = new Student[]
{
new Student("ID1", "Name1"),
new Student("ID2", "Name2"),
new Student("ID3", "Name3"),
}; TestEnumerable testEnumerable = new TestEnumerable(students); foreach (Student s in testEnumerable)
{
Console.WriteLine("ID:{0};Name:{1}", s.ID, s.Name);
}
}
如上文所说, TestEnumerable类用foreach枚举,得到的每一个对象就是上文提到的“可以枚举的元素(Student[])的每一个对象” 。
IEnumerator接口:枚举的真正实现
public interface IEnumerator
{
//获取集合中的当前元素
object Current{get;set;} //枚举数推进到集合的下一个元素,当存在下一个对象时返回true,不存在时返回false
bool MoveNext(); //枚举数设置为其初始位置,该位置位于集合中第一个元素之前
void Reset();
}
public class TestEnumerator : IEnumerator
{
//指针,指向枚举元素 (为什么这里是-1,因为在MoveNext()中,_position++)
private int _position = -;
private Student[] _students;
public TestEnumerator() { }
public TestEnumerator(Student[] list)
{
_students = list;
} /// <summary>
/// 获取当前元素
/// </summary>
public object Current
{
get
{
return _students[_position];
}
} /// <summary>
/// 移动到下一个元素
/// </summary>
/// <returns>成功移动到下一个元素返回:true;失败返回:false</returns>
public bool MoveNext()
{
_position++;
return _position < _students.Length;
} /// <summary>
/// 重置到第一个元素
/// </summary>
public void Reset()
{
_position = -;
}
} public class TestEnumerable2 : IEnumerable
{
private Student[] _students;
public TestEnumerable2(Student[] list)
{
_students = list;
} public IEnumerator GetEnumerator()
{
return new TestEnumerator(_students);
}
}
调用代码:
public static void Main()
{
Student[] students = new Student[]
{
new Student("ID1", "Name1"),
new Student("ID2", "Name2"),
new Student("ID3", "Name3"),
}; TestEnumerable2 testEnumerator = new TestEnumerable2(students); foreach (Student s in testEnumerator)
{
Console.WriteLine("ID:{0};Name:{1}", s.ID, s.Name);
}
}
IEnumerable接口与IEnumerator接口算介绍完毕了。
大家看IEnumerator接口的实现,就是实现一个属性,两个调整枚举点的方法。如果能有一种方式,能自动帮我们完成IEnumerator接口的实现,那么将方便很多。
在.NET Framework 3.5中有了yield,这个关键字可以帮我们自动生成实现IEnumerator的类。
yield
yield关键字,可以帮我们自动生成一个实现了IEnumerator的类,来完成枚举。
下面,我们先来看一下MSDN上的介绍
1.foreach 循环的每次迭代都会调用迭代器方法
2.迭代器方法运行到 yield return 语句时,会返回一个expression表达式并保留当前在代码中的位置
3.当下次调用迭代器函数时执行从该位置重新启动
其实msdn这里解释的有点啰嗦了。简单点说,就是迭代器运行到yield return语句时,将自动做两件事。
1.记录当前访问的位置
2.当下次调用迭代器函数时执行从该位置重新启动:MoveNext()
下面我们来看一段实例代码:
public class TestEnumerable3 : IEnumerable
{
private Student[] _students; public TestEnumerable3(Student[] list)
{
_students = list;
} //实现与之前有不同
public IEnumerator GetEnumerator()
{
//迭代集合
foreach (Student stu in _students)
{
//返回当前元素,剩下的就交给yield了,它会帮我们生成一个实现了接口IEnumerator的类
//来帮我们记住当前访问到哪个元素。
yield return stu;
}
}
}
下面我贴出一段GetEnumerator()方法的IL代码,来证明yield return 帮我们自动生成了一个实现了IEnumerator接口的类
.method public final hidebysig newslot virtual
instance class [mscorlib]System.Collections.IEnumerator GetEnumerator () cil managed
{
// Method begins at RVA 0x228c
// Code size 20 (0x14)
.maxstack
.locals init (
[] class TestBlog.TestEnumerable3/'<GetEnumerator>d__0', //这句最重要,初始化了一个实现System.Collections.IEnumerator的class,存储在索引1的位置
[] class [mscorlib]System.Collections.IEnumerator
) IL_0000: ldc.i4.
IL_0001: newobj instance void TestBlog.TestEnumerable3/'<GetEnumerator>d__0'::.ctor(int32)
IL_0006: stloc.
IL_0007: ldloc.
IL_0008: ldarg.
IL_0009: stfld class TestBlog.TestEnumerable3 TestBlog.TestEnumerable3/'<GetEnumerator>d__0'::'<>4__this'
IL_000e: ldloc.
IL_000f: stloc.
IL_0010: br.s IL_0012 IL_0012: ldloc.
IL_0013: ret
} // end of method TestEnumerable3::GetEnumerator
最后就是调用代码:
static void Main(string[] args)
{
Student[] students = new Student[]
{
new Student("ID1", "Name1"),
new Student("ID2", "Name2"),
new Student("ID3", "Name3"),
}; TestEnumerable3 testEnumerable3 = new TestEnumerable3(students); foreach (Student s in testEnumerable3)
{
Console.WriteLine("ID:{0};Name:{1}", s.ID, s.Name);
}
}
IEnumerable构建迭代方法
/// <summary>
/// IEnumerable构建迭代方法
/// <param name="_students">排序集合</param>
/// <param name="direction">排序顺序(true:顺序;false:倒序)</param>
public IEnumerable SortStudents(Student[] _students,bool direction)
{
if (direction)
{
foreach (Student stu in _students)
{
yield return stu;
}
}
else
{
for (int i = _students.Length; i != ; i--)
{
yield return _students[i - ];
}
}
}
细心的朋友一定发现了。这个方法的返回类型为IEnumerable,而不像实现接口IEnumerable的GetEnumerator()方法返回类型是IEnumerator。这就是IEnumerable构建迭代方法需要注意的地方。
下面是测试代码:
static void Main(string[] args)
{
Student[] students = new Student[]
{
new Student("ID1", "Name1"),
new Student("ID2", "Name2"),
new Student("ID3", "Name3"),
}; foreach (Student s in SortStudents(students,false))
{
Console.WriteLine("ID:{0};Name:{1}", s.ID, s.Name);
}
}
感谢大家的耐心阅读。
IEnumerable与IEnumerator的更多相关文章
- 细说 C# 中的 IEnumerable和IEnumerator接口
		
我们先思考几个问题: 为什么在foreach中不能修改item的值? 要实现foreach需要满足什么条件? 为什么Linq to Object中要返回IEnumerable? 接下来,先开始我们的正 ...
 - 迭代器学习之一:使用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的区别
		
首先是IEnumerable与IEnumerator的定义: 1.IEnumerable接口允许使用foreach循环,包含GetEnumerator()方法,可以迭代集合中的项. 2.IEnumer ...
 - IEnumerable和IEnumerator 详解 (转)
		
原文链接:http://blog.csdn.net/byondocean/article/details/6871881 参考链接:http://www.cnblogs.com/hsapphire/a ...
 - C#基础知识系列九(对IEnumerable和IEnumerator接口的糊涂认识)
		
前言 IEnumerable.IEnumerator到现在为止对这两个接口还是不太理解,不理解但是自己总是想着试着要搞明白,毕竟自己用的少,所以在此先记录一下.以备自己日后可以来翻查,同时也希望园子里 ...
 - [转]那些年我还不懂:IList,ICollection,IEnumerable,IEnumerator,IQueryable
		
1.首先看一个简单的例子 int[] myArray = { 1, 32, 43, 343 }; IEnumerator myie = myArray.GetEnumerator(); myie.Re ...
 - 转载IEnumerable与IEnumerator区别
		
public interface IEnumerable { IEnumerator GetEnumerator(); } public interface IEnumerator { ...
 - IEnumerable、IEnumerator与yield的学习
		
我们知道数组对象可以使用foreach迭代进行遍历,同时我们发现类ArrayList和List也可以使用foreach进行迭代.如果我们自己编写的类也需要使用foreach进行迭代时该怎么办呢? IE ...
 
随机推荐
- 014-Spring Boot web【三】拦截器HandlerInterceptor、异常处理页面,全局异常处理ControllerAdvice
			
一.拦截器HandlerInterceptor 1.1.HandlerInterceptor接口说明 preHandle,congtroller执行前,如果返回false请求终端 postHandle ...
 - java-JProfiler(三)-进行本地JVM的性能监控-监视本地java程序
			
1.打开JProfiler 默认会启动快速窗口[或者使用菜单Session→Start Center]打开 这里监视本地java程序,故在 主界面 2.查看监视界面 这时就可以查看 Instance ...
 - SSH进行登录远程主机,实验室网站,项目
			
1:下载putty,双击putty.exe http://www.putty.be/latest.html 输入要连接的ip和端口号,直接打开open.或者在saved Sessions框里面 ...
 - 使用SolrJ代码导入,发布搜索服务
			
搭建solr服务器:http://www.cnblogs.com/liyafei/p/8005571.html 一导入要搜索的字段 1:确定发布搜索的字段,sql语句 SELECT a.id, b. ...
 - 包管理 ----- Linux操作系统rpm包安装方式步骤
			
Linux操作系统rpm包安装方式步骤 2016年08月04日 07:00:26 阅读数:17140 转自 : http://os.51cto.com/art/201003/186467.htm 特别 ...
 - 最大流(EK)
			
最大流 — Edmond Karp算法 Edmond Karp算法的大概思想: 反复寻找源点s到汇点t之间的增广路径,若有,找出增广路径上每一段[容量-流量]的最小值delta,若无,则结束. 在寻找 ...
 - [Leetcode] 49. Group Anagrams_Medium
			
Given an array of strings, group anagrams together. Example: Input: ["eat", "tea" ...
 - testng入门教程4用TestNG执行case
			
使用TestNG类执行测试用例.这个类的主入口点在TestNG的框架运行测试.用户可以创建自己的TestNG的对象,并调用它以许多不同的方式: 在现有的testng.xml 合成testng.xml, ...
 - smali注入常用代码
			
注入代码需要注意寄存器个数.1.插入log信息 const-string v2,"SN" invoke-static {v2,v0}, Landroid/util/Log;-> ...
 - angular前端框架
			
总所周知,在前端开发中,大家用的比较多的框架就是angular,vue,react等,今天就为大家讲一下angular大家框架的原理及运用 1.本次所举的例子是以依赖require.js的, < ...