IEnumerable、IEnumerator接口(如何增加迭代器功能)
IEnumerable、IEnumerator接口封装了迭代器功能,有了它,我们不需要将内部集合暴露出去,外界只需要访问我的迭代器接口方法即可遍历数据。
在C#中,使用foreach语句来遍历集合。foreach语句是微软提供的语法糖,使用它可以简化C#内置迭代器的使用复杂性。编译foreach语句,会生成调用GetEnumerator和MoveNext方法以及Current属性的代码。
有两种实现迭代器的方式,一是自己继承IEnumerable接口,比较复杂;二是使用yield,简化操作,下面会有案例。
反编译foreach,生成类似下面这段代码:
IEnumerator<Student> studentEnumerator = studentList.GetEnumerator();
while (studentEnumerator.MoveNext())
{
var currentStudent = studentEnumerator.Current as Student;
Console.WriteLine("Id = {0}, Name = {1}, Age = {2}", currentStudent.Id, currentStudent.Name, currentStudent.Age);
}
案例1:给自己的类增加迭代器功能
public class StudentSet : IEnumerable
{
private Student[] students;
public StudentSet(Student[] inputStudents)
{
students = new Student[inputStudents.Length];
for (int i = ; i < inputStudents.Length; i++)
{
students[i] = inputStudents[i];
}
} IEnumerator IEnumerable.GetEnumerator()
{
return (IEnumerator)GetEnumerator();
} public StudentEnumerator GetEnumerator()
{
return new StudentEnumerator(students);
}
} public class StudentEnumerator : IEnumerator
{
public Student[] students;
int position = -;
public StudentEnumerator(Student[] students)
{
this.students = students;
} public bool MoveNext()
{
position++;
return (position < students.Length);
} public void Reset()
{
position = -;
} object IEnumerator.Current
{
get
{
return Current;
}
} public Student Current
{
get
{
try
{
return students[position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
}
案例2:使用yield实现迭代器功能
public class MyCollection
{
private int[] list = new int[] { , , , }; public IEnumerator<int> GetEnumerator()
{
for (int i = ; i < list.Length; i++)
{
yield return list[i];
}
}
}
MyCollection col = new MyCollection();
foreach (var item in col)
{
Console.WriteLine(item);
}
IEnumerable、IEnumerator接口(如何增加迭代器功能)的更多相关文章
- IEnumerable, IEnumerator接口
IEnumerable接口 // Exposes the enumerator, which supports a simple iteration over a non-generic collec ...
- C# ~ 从 IEnumerable / IEnumerator 到 IEnumerable<T> / IEnumerator<T> 到 yield
IEnumerable / IEnumerator 首先,IEnumerable / IEnumerator 接口定义如下: public interface IEnumerable /// 可枚举接 ...
- 迭代器学习之一:使用IEnumerable和IEnumerator接口
写博客是检验我学习的成果之一以及自我总结的一种方式,以后会经常利用这种方式进行技术交流和自我总结,其中认识不深难免会有错误,但是一直懂得不懂就问,不懂就学的道理! 1.首先看一个简单的列子 , , , ...
- C# 通过IEnumberable接口和IEnumerator接口实现自定义集合类型foreach功能
1.IEnumerator和IEnumerable的作用 其实IEnumerator和IEnumerable的作用很简单,就是让除数组和集合之外的类型也能支持foreach循环,至于foreach循环 ...
- C# 通过IEnumberable接口和IEnumerator接口实现泛型和非泛型自定义集合类型foreach功能
IEnumerator和IEnumerable的作用 其实IEnumerator和IEnumerable的作用很简单,就是让除数组和集合之外的类型也能支持foreach循环,至于foreach循环,如 ...
- 细说 C# 中的 IEnumerable和IEnumerator接口
我们先思考几个问题: 为什么在foreach中不能修改item的值? 要实现foreach需要满足什么条件? 为什么Linq to Object中要返回IEnumerable? 接下来,先开始我们的正 ...
- C#基础知识系列九(对IEnumerable和IEnumerator接口的糊涂认识)
前言 IEnumerable.IEnumerator到现在为止对这两个接口还是不太理解,不理解但是自己总是想着试着要搞明白,毕竟自己用的少,所以在此先记录一下.以备自己日后可以来翻查,同时也希望园子里 ...
- C# IEnumerable 和 IEnumerator接口浅析
温故而知新,可以为师矣,有空经常复习一下基础知识是有必要的,并且能加深理解和记忆. Foreach常用于循环访问集合,对实现IEnumerable的接口的容器进行遍历,IEnumerable和IEnu ...
- IEnumerable和IEnumerator接口
我们先思考几个问题:1.为什么在foreach中不能修改item的值?(IEnumerator的Current为只读)2.要实现foreach需要满足什么条件?(实现IEnumerator接口来实现的 ...
随机推荐
- Visual Studio 调试系列5 检查变量(使用自动窗口和局部变量窗口)
系列目录 [已更新最新开发文章,点击查看详细] 在调试时,“自动变量”和“局部变量”窗口会显示变量值. 仅在调试会话期间,这两个窗口才可用. “自动变量”窗口显示当前断点周围使用的变量. “局 ...
- Collection和Collections有什么区别?
本文链接:https://blog.csdn.net/xiangyuenacha/article/details/84237663 1.java.util.Collection 是一个集合接口 ...
- 《Game Programming Patterns》游戏设计模式
转载自:https://blog.csdn.net/poem_qianmo/article/details/52505170 https://blog.csdn.net/poem_qianmo/art ...
- 部门工资前三高的所有员工 - LeetCode
Employee 表包含所有员工信息,每个员工有其对应的工号 Id,姓名 Name,工资 Salary 和部门编号 DepartmentId . +----+-------+--------+---- ...
- 如何解决macbook pro摄像头不工作的问题
背景:上周用qq视频聊天都正常,这周突然显示检测不到摄像头.打开facetime和photo booth也显示“相机未连接”排查一切问题后只好给苹果客服打电话,在客服的帮助下解决了这个问题. 解决办法 ...
- 发送邮件报错javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed
关于使用javaMail发送邮件报错:javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multip ...
- Ubuntu下SVN客户端RapidSVN
Window下我们使用TortoiseSVN,可以很方便地进行查看.比较.更新.提交.回滚等SVN版本控制操作.在Linux下,我们可以使用rapidsvn. RapidSVN是一款不错的SVN客户端 ...
- 快速学会使用Vuex
一.Vuex简介 官方定义 Vuex是一个专门为Vue.js应用程序开的状态管理模式 它采用集中式存储管理应用的所有组件的状态 并以相应的规则保证以一种可预测的方式发生变化 二.应用场景 多个视图依赖 ...
- Mysql系列(九)—— 性能分析explain执行计划
explain是mysql中sql优化的一个重要手段.顾名思义,explain就是解释sql,用于表示sql是怎样执行的信息,即sql执行计划! 语法 explain statement statem ...
- [转] vue 自定义组件使用v-model
<input v-model="something"> v-model指令其实是下面的语法糖包装而成: <input :value="something ...