C#中的IEnumerable简介及简单实现实例
作者:junjie 时间:2023-07-15 22:27:07
IEnumerable这个接口在MSDN上是这么说的,它是一个公开枚举数,该枚举数支持在非泛型集合上进行简单的迭代。换句话说,对于所有数组的遍历,都来自IEnumerable,那么我们就可以利用这个特性,来定义一个能够遍历字符串的通用方法.
下面先贴出code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace mycs
{
class Program
{
static void Main(string[] args)
{
charlist mycharlist = new charlist("hello world");
foreach (var c in mycharlist)
{
Console.Write(c);
}
Console.ReadLine();
}
}
class charlist : IEnumerable
{
public string TargetStr { get; set; }
public charlist(string str)
{
this.TargetStr = str;
}
public IEnumerator GetEnumerator()
{
//c# 1.0
return new CharIterator(this.TargetStr);
//c# 2.0
/*
for (int index = this.TargetStr.Length; index > 0;index-- )
{
yield return this.TargetStr[index - 1];
}
*/
}
}
class CharIterator : IEnumerator
{
public string TargetStr { get; set; }
public int position { get; set; }
public CharIterator(string targetStr)
{
this.TargetStr = targetStr;
this.position = this.TargetStr.Length;
}
public object Current
{
get
{
if (this.position==-1||this.position==this.TargetStr.Length)
{
throw new InvalidOperationException();
}
return this.TargetStr[this.position];
}
}
public bool MoveNext()
{
if (this.position!=-1)
{
this.position--;
}
return this.position > -1;
}
public void Reset()
{
this.position = this.TargetStr.Length;
}
}
}
在上面的例子c# 1.0中,CharIterator就是迭代器的实现,position字段存储当前的迭代位置,通过Current属性可以得到当前迭代位置的元素,MoveNext方法用于更新迭代位置,并且查看下一个迭代位置是不是有效的。
当我们通过VS单步调试下面语句的时候:
foreach (var c in charList)
代码首先执行到foreach语句的charList处获得迭代器CharIterator的实例,然后代码执行到in会调用迭代器的MoveNext方法,最后变量c会得到迭代器Current属性的值;前面的步骤结束后,会开始一轮新的循环,调用MoveNext方法,获取Current属性的值。
通过C# 1.0中迭代器的代码看到,要实现一个迭代器就要实现IEnumerator接口,然后实现IEnumerator接口中的MoveNext、Reset方法和Current属性。
在C# 2.0中可以直接使用yield语句来简化迭代器的实现。
如上面public IEnumerator GetEnumerator()方法中注释掉的部分。
通过上面的代码可以看到,通过使用yield return语句,我们可以替换掉整个CharIterator类。
yield return语句就是告诉编译器,要实现一个迭代器块。如果GetEnumerator方法的返回类型是非泛型接口,那么迭代器块的生成类型(yield type)是object,否则就是泛型接口的类型参数。
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android 类似UC浏览器的效果:向上滑动地址栏隐藏功能
SpringBoot整合Mybatis实现CRUD
![](https://img.aspxhome.com/file/2023/5/84765_0s.png)
javaweb实现在线支付功能
![](https://img.aspxhome.com/file/2023/8/93348_0s.jpg)
C#中方法的详细介绍
Flutter 实现下拉刷新上拉加载的示例代码
![](https://img.aspxhome.com/file/2023/8/88668_0s.gif)
struts2 validation.xml 验证规则代码解析
SpringBoot打War包上传到阿里云的LINUX服务器的操作方法
![](https://img.aspxhome.com/file/2023/7/84197_0s.jpg)
浅扒Android动态设置字体大小的示例
Flutter开发通用页面Loading组件示例详解
Java中常用的6种排序算法详细分解
C#实现简单俄罗斯方块
![](https://img.aspxhome.com/file/2023/5/66575_0s.jpg)
详解Android自定义控件属性
![](https://img.aspxhome.com/file/2023/7/126767_0s.png)
kafka消费者kafka-console-consumer接收不到数据的解决
![](https://img.aspxhome.com/file/2023/3/116113_0s.png)
flutter ExpansionTile 层级菜单的实现
![](https://img.aspxhome.com/file/2023/7/91007_0s.png)
使用SpringBoot获取所有接口的路由
Android如何自定义按钮效果
Java基础之创建虚拟机对象的过程详细总结
![](https://img.aspxhome.com/file/2023/4/128924_0s.png)
go打包aar及flutter调用aar流程详解
![](https://img.aspxhome.com/file/2023/4/87634_0s.png)