当前位置:网站首页>C graphical tutorial (Fourth Edition)_ Chapter 18 enumerator and iterator: enumerator samplep340
C graphical tutorial (Fourth Edition)_ Chapter 18 enumerator and iterator: enumerator samplep340
2022-07-03 12:52:00 【superfreak】
using System;
using System.Collections;
namespace Chapter18EnumeratorSampleP340
{
class ColorEnumerator:IEnumerator
{
string[] _colors;
int _position = -1;
public ColorEnumerator(string[] theColors)// Constructors
{
_colors = new string[theColors.Length];
for (int i=0;i<theColors.Length;i++)
_colors[i] = theColors[i];
}
public object Current// Realization Current Get the value of the element at the current position in the collection
{
get
{
if (_position == -1)
throw new InvalidOperationException();
if (_position >= _colors.Length)
throw new InvalidOperationException();
return _colors[_position];
}
}
public bool MoveNext()// Realization MoveNext(): Let the iterator move on to the next element , Successfully returns true, conversely return false
{
if (_position < _colors.Length - 1)
{
_position++;
return true;
}
else
return false;
}
public void Reset()// Realization Reset: Set the enumerator to return to its original position , Before the first element
{
_position = -1;
}
}
class Spectrum :IEnumerable// Interface System.Collections.IEnumerator, It is exposed that an enumerator supports simple iterations ,
// In a non generic set . You have to implement functions GetEnumerator()
{
string[] Colors = { "violet", "blue", "cyan", "green", "yellow", "orange", "red" };
public IEnumerator GetEnumerator()
{
return new ColorEnumerator(Colors);
}
}
class Program
{
static void Main(string[] args)
{
Spectrum spectrum = new Spectrum();
foreach (string color in spectrum)
Console.WriteLine(color);
Console.WriteLine("Hello World!");
}
}
}
边栏推荐
- Quick learning 1.8 front and rear interfaces
- Powerful avatar making artifact wechat applet
- Sword finger offer05 Replace spaces
- Project video based on Linu development
- Idea packages the web project into a war package and deploys it to the server to run
- 低代码平台国际化多语言(i18n)技术方案
- 自抗扰控制器七-二阶 LADRC-PLL 结构设计
- [exercise 7] [Database Principle]
- Enter the length of three sides of the triangle through the user, and calculate the area of the triangle, where the length is a real number
- Xctf mobile--app1 problem solving
猜你喜欢
剑指Offer03. 数组中重复的数字【简单】
[problem exploration and solution of one or more filters or listeners failing to start]
Ali & ant self developed IDE
With pictures and texts, summarize the basic review of C language in detail, so that all kinds of knowledge points are clear at a glance?
ncnn神經網絡計算框架在香柳丁派OrangePi 3 LTS開發板中的使用介紹
Eureka自我保护
Quick learning 1.8 front and rear interfaces
Analysis of a music player Login Protocol
Attack and defense world mobile--ph0en1x-100
电压环对 PFC 系统性能影响分析
随机推荐
The latest version of blind box mall thinkphp+uniapp
Xctf mobile--app3 problem solving
[problem exploration and solution of one or more filters or listeners failing to start]
Public and private account sending prompt information (user microservice -- message microservice)
The foreground uses RSA asymmetric security to encrypt user information
4. 无线体内纳米网:电磁传播模型和传感器部署要点
Brief introduction to mvcc
Everything comes to him who waits
Ali & ant self developed IDE
剑指Offer04. 二维数组中的查找【中等】
[comprehensive question] [Database Principle]
Node. Js: use of express + MySQL
The upward and downward transformation of polymorphism
01 three solutions to knapsack problem (greedy dynamic programming branch gauge)
Differences and connections between final and static
Sword finger offer04 Search in two-dimensional array [medium]
ORM use of node -serialize
【数据库原理复习题】
Harmonic current detection based on synchronous coordinate transformation
Four problems and isolation level of MySQL concurrency