当前位置:网站首页>Implementation of C iterator
Implementation of C iterator
2022-07-26 01:54:00 【Tingyi -- flying bird】

1. Code
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lesson23_Iterator
{
class Program
{
#region One 、 What is an iterator
// Iterator pattern Provide a method to sequentially access The elements
// have access to foreach Traversal class , All implement iterators
#endregion
#region Two 、 What does a standard iterator need to implement
// Key interfaces :IEnumerable,
// Namespace :using System.Collections;
class MyList : IEnumerable, IEnumerator
{
private int[] list;
private int cursor = -1;
public MyList()
{
list = new int[] {
1, 2, 3, 4};
}
// foreach The essence
// 1. call GetEnumerator Method obtain
// 2. call MoveNext()
// 3.MoveNext() return true Get Current
// Again MoveNext()
// If you return False end And call Reset()
#region IEnumerable
public IEnumerator GetEnumerator()
{
// First cursor position
Reset();
// Return your own object
return this;
}
#endregion
#region IEnumerator
// return Aggregating object
public object Current
{
get
{
return list[cursor];
}
}
// Move the cursor
public bool MoveNext()
{
// Move the cursor
++cursor;
// Overflow is illegal
return cursor < list.Length;
}
// Reset cursor
public void Reset()
{
cursor=-1;
}
#endregion
}
#endregion
#region 3、 ... and 、 Use yield return Implementation iterator
// 1. Inherit IEnumerable An interface
// 2. Use yield return return item that will do
class CustomList : IEnumerable
{
private int[] list;
public CustomList()
{
this.list = new int[] {
5, 6, 7, 8 };
}
public IEnumerator GetEnumerator()
{
for (int i = 0; i < list.Length; i++)
{
// Grammatical sugar The specific implementation after compilation is The traditional way
yield return this.list[i];
}
}
}
#region Four 、 Generic iterator
// 1. Inherit IEnumerable An interface
// 2. Use yield return return item that will do
class TempleList<T> : IEnumerable
{
private T[] arry;
public TempleList(params T[] arry)
{
this.arry = arry;
}
public IEnumerator GetEnumerator()
{
for (int i = 0; i < this.arry.Length; i++)
{
yield return this.arry[i];
}
}
}
#endregion
#endregion
static void Main(string[] args)
{
Console.WriteLine("========= iterator ===========");
MyList mylist = new MyList();
Console.WriteLine("=========1. Traditional realization ===========");
foreach (int item in mylist)
{
Console.WriteLine(item);
}
Console.WriteLine("=========2.yield Realization ===========");
CustomList customList = new CustomList();
foreach (int item in customList)
{
Console.WriteLine(item);
}
Console.WriteLine("=========3.yield Implement generic iterators ===========");
TempleList<string> tempList = new TempleList<string>("AAA", "BBB", "CCC");
foreach (var item in tempList)
{
Console.WriteLine(item);
}
// summary
// An iterator is You can go directly through foreach Traversing objects , Without knowing the internal results
// The main implementation methods are :
// 1. traditional method Inherit two interfaces IEnumerable, IEnumerator
// 2.yield return How to achieve Inherit IEnumerable
Console.ReadLine();
}
}
}

边栏推荐
- excel中怎么显示数字/英文时间
- "Wei Lai Cup" 2022 Niuke summer multi school training camp 2 personal problem sets
- MPLS knowledge points
- There is no setter method in grpc list under flutter. How to use related attributes
- BGP knowledge points summary
- 旅行 (拆点分层)
- 学习笔记:原码, 反码, 补码
- pdf. JS introduction
- 元素和小于等于阈值的正方形的最大边长(来源:力扣(LeetCode))
- Video game quiz? I think it's useless. It's better to do these well!
猜你喜欢

# Dest0g3 520迎新赛(更新中)

Dest0g3 520 orientation (under update)

What is a test case? How to design?

IDEA如何快速删除最近打开的项目

Worthington木瓜蛋白酶丨从纯化的蛋白聚糖生产糖肽(附文献)

3、 Pinda general permission system__ pd-tools-swagger2

pt-onnx-ncnn转换的问题记录(接yolov5训练)

Image batch processing Gaussian filter noise reduction + peak signal-to-noise ratio calculation

AutoCAD -- Method of calculating area

flutter 下 grpc list没有Setter 方法 ,如何使用相关属性
随机推荐
【深入浅出玩转FPGA学习11----Testbench书写技巧2】
怎么使用宝塔面板把node全栈项目部署到服务器上
Y77. Chapter IV Prometheus' monitoring system and practice -- Prometheus' service discovery mechanism (VIII)
"Weilai Cup" 2022 Niuke summer multi school training camp 2 h.[take the elevator] maintenance section
The detailed knowledge summary of MySQL can be collected
Navica工具把远程MySQL导入到本地MySQL数据库
Worthington产气荚膜梭菌神经氨酸酶的特征及测定
PHP Alipay transfer to Alipay account
给RestTemplate添加拦截器记录请求响应,还需解决流只读一次的问题
2022 love analysis ― bank digitalization practice report
Protect syslog servers and devices
pt-onnx-ncnn转换的问题记录(接yolov5训练)
G2. passable paths (hard version) (tree diameter + LCA)
【深入浅出玩转FPGA学习11----Testbench书写技巧1】
Worthington核酸酶、微球菌相关研究及测定方案
opengauss如何手工安装(非OM方式)
NFT access tool premint was hacked and lost more than 370000 US dollars
What is a test case? How to design?
IP address of the network
Qtreewidget dotted line setting