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 ...
随机推荐
- TCP/UDP client/server library for Java, 最好的java语言tcp udp 服务器客户端实现库
这个库andrdoi也可以用,而且是基于类的使用方式: 它支持类似聊天室的功能,即一个人说话,所有客户端都能收到,当然也支持点点通信.它还支持 RMI 的方式调用远程过程. https://githu ...
- Chrome Input框老是有输入记录的终极解决方案
尤其是日期框,输入记录都挡住日期弹框了. 浏览器地址栏输入: chrome://settings/autofill,按钮关掉就可以了.
- end=‘’
print('----------------') a = ['aa','bb','cc','dd','ee'] for i in range(len(a)): print(i,a[i])#默认换行 ...
- js数组之从数组中删除元素
使用pop()这个函数可以从数组中删除末尾的元素,shift方法可以删除数组中第一个元素.这些都是js中自带的函数,如果不使用这些函数的话,自己写的代码效率会很低的. <html> < ...
- C#中DataTable
.C#中DataTable技术学习 2009-09-10 14:37:18 阅读1496 评论0 字号:大中小 订阅 . 1.在DataTable中执行DataTable.Select(" ...
- mysql回滚日志
一.回滚日志(undo log) 1.作用 保存了事务发生之前的数据的一个版本,可以用于回滚,同时可以提供多版本并发控制下的读(MVCC),也即非锁定读 2.内容 逻辑格式的日志,在执行undo的时候 ...
- python爬虫对于gb2312
对于刚刚接触python爬虫的人,常常会碰到一个比较烦的问题, 如果网页是GB2312编码格式,我们直接decode(’GB2312‘)一般python都会报错: GB2312不能编码该页面. 这就比 ...
- Object-C-NSFileHandle
NSFileHandle 类中得到方法可以很方便的对文件数据进行读写.追加,以及偏移量的操作. NSFileHandle 基本步骤: 1.打开文件,获取一个NSFileHandle 对象 2.对打开N ...
- iOS 建立项目过滤机制 —— 给工程添加忽略文件.gitignore
目前iOS 项目 主要忽略 临时文件.配置文件.或者生成文件等,在不同开发端这些文件会大有不同,如果 git add .把这些文件都push到远程, 就会造成不同开发端频繁改动和提交的问题. ...
- suse zypper 添加源
一.查看源和仓库 1.查看repos (软件仓库) zypper lr 2.查看services(软件源) zypper ls 二.删除源和仓库 1.删除软件仓库 zypper rr name 2.删 ...