-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter18.cs
More file actions
110 lines (91 loc) · 2.64 KB
/
Chapter18.cs
File metadata and controls
110 lines (91 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using System.Collections;
namespace Chapter18
{
class ColorEnumerator : IEnumerator
{
string[] _colors;
int _position = -1;
public ColorEnumerator(string[] theColors)
{
_colors = new string[theColors.Length];
for(int i = 0; i < theColors.Length; ++i)
{
_colors[i] = theColors[i];
}
}
public object Current
{
get
{
if(_position == -1)
throw new InvalidOperationException();
if(_position >= _colors.Length)
throw new InvalidOperationException();
return _colors[_position];
}
}
public bool MoveNext()
{
if(_position < _colors.Length - 1)
{
_position++;
return true;
}
else
return false;
}
public void Reset()
{
_position = -1;
}
}
class Spectrum : IEnumerable
{
string[] Colors = {"violet", "blue", "cyan", "green", "yellow", "orange", "red"};
public IEnumerator GetEnumerator()
{
return new ColorEnumerator(Colors);
}
}
class MyClass
{
public IEnumerator GetEnumerator()
{
return BlackAndWhite();
}
public IEnumerator BlackAndWhite()
{
yield return "black";
yield return "gray";
yield return "white";
}
}
class Program
{
static void MainTest()
{
Console.WriteLine("2018-4-18");
int[] arr1 = {10,11,12,13};
foreach(int item in arr1)
Console.WriteLine("Item value: {0}", item);
Console.WriteLine("********************************************");
int[] MyArray = {10,11,12,13};
IEnumerator ie = MyArray.GetEnumerator();
while(ie.MoveNext())
{
int i = (int)ie.Current;
Console.WriteLine("{0}", i);
}
Console.WriteLine("********************************************");
Spectrum spectrum = new Spectrum();
foreach(string color in spectrum)
Console.WriteLine(color);
Console.WriteLine("********************************************");
MyClass mc = new MyClass();
foreach(string shade in mc)
Console.WriteLine(shade);
Console.WriteLine("2018-4-19");
}
}
}