C#迭代器,枚举器
来源:互联网 发布:淘宝李白凤求凰可信吗 编辑:程序博客网 时间:2024/04/30 00:07
测试代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace _07迭代器
{
class GameMoves
{
private IEnumerator cross;
private IEnumerator circle;
public GameMoves() {
cross = Cross();
circle = Circle();
}
private int move = 0;
const int MaxMoves = 9;
public IEnumerator Cross() {
while (true)
{
Console.WriteLine("Cross,move {0}", move);
if (++move >= MaxMoves)
{
yield break;
}
yield return circle;
}
}
public IEnumerator Circle()
{
while (true)
{
Console.WriteLine("Circle,move {0}", move);
if (++move >= MaxMoves)
{
yield break;
}
yield return cross;
}
}
}
class Program
{
static void Main(string[] args)
{
var game = new GameMoves();
IEnumerator enumerator = game.Cross();
while (enumerator.MoveNext())
{
enumerator = enumerator.Current as IEnumerator;
}
Console.ReadKey();
}
}
}
yield return 每次返回一个枚举器,包含yield的方法或属性叫做迭代快,使用迭代快,编译器会生成一个yield类型,MoveNext()方法封装了迭代块中的代码
Current属性返回yield return 中返回的内容。这就是迭代器执行的顺序。
- C#迭代器,枚举器
- C# 迭代器实现枚举器
- C# 枚举器 手动实现枚举器 可枚举集合 枚举器操作 迭代器
- C#枚举器,手动实现枚举器,可枚举集合,枚举器操作,迭代器
- C# 枚举器 手动实现枚举器 可枚举集合 枚举器操作 迭代器
- C# 中的枚举器
- C# 中的枚举器
- C# 中的枚举器
- C# 中的枚举器
- C# 中的枚举器
- C# 中的枚举器
- C# 枚举器
- C#枚举
- C#枚举
- C# - 枚举
- c# 枚举
- C#枚举
- C# 枚举
- 后可视化编程
- 旧工程适配iOS6和iPhone5的一些故事
- POJ 1903 - Jurassic Remains 中途相遇法(枚举)
- 2011级-csdn-java-张侃—Spring(2)
- BlazeDS
- C#迭代器,枚举器
- ComboBox控件 详解1
- 查看图片前进后退效果图
- hdu 1025 Constructing Roads In JGShining's Kingdom
- Hibernate插入、查询、删除操作
- 在线进销存管理系统源码 客户关系管理系统源码 人事薪资管理系统源码(PHP+MySQL)
- <jsp:include>能include静态页面吗?
- JDBC中编写的经典通用的工具类
- 扫盲贴(二)----------------------专业术语