当前位置:网站首页>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!");
}
}
}
边栏推荐
猜你喜欢

initial、inherit、unset、revert和all的区别

Seven second order ladrc-pll structure design of active disturbance rejection controller

Xctf mobile--app2 problem solving

4. 无线体内纳米网:电磁传播模型和传感器部署要点

TOGAF认证自学宝典V2.0

Eureka self protection

(最新版) Wifi分销多开版+安装框架

Four problems and isolation level of MySQL concurrency

idea将web项目打包成war包并部署到服务器上运行
![Sword finger offer04 Search in two-dimensional array [medium]](/img/c4/002c951f8d914aaea4f4133685ebd1.png)
Sword finger offer04 Search in two-dimensional array [medium]
随机推荐
Swift Error Handling
Use Tencent cloud IOT platform to connect custom esp8266 IOT devices (realized by Tencent continuous control switch)
【ManageEngine】IP地址扫描的作用
Enable SASL authentication for memcached
剑指Offer04. 二维数组中的查找【中等】
01 three solutions to knapsack problem (greedy dynamic programming branch gauge)
Ten workplace rules
并网-低电压穿越与孤岛并存分析
Harmonic current detection based on synchronous coordinate transformation
Kung Fu pays off, and learning is done
Four problems and isolation level of MySQL concurrency
【ArcGIS自定义脚本工具】矢量文件生成扩大矩形面要素
Approve iPad, which wants to use your icloud account
Analysis of the influence of voltage loop on PFC system performance
Swift5.7 extend some to generic parameters
The upward and downward transformation of polymorphism
Idea packages the web project into a war package and deploys it to the server to run
Drop down refresh conflicts with recyclerview sliding (swiperefreshlayout conflicts with recyclerview sliding)
Express abstract classes and methods
Quickly learn member inner classes and local inner classes