C#
foreach를 사용할 수 있는 일반화 클래스
JAESEONG LEE- developer
2022. 7. 25. 19:28
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
111
112
113
|
using System;
using System.Collections;
using System.Collections.Generic;
namespace ee
{
class MyList<T> : IEnumerable<T>, IEnumerator<T>
{
private T[] array;
int position = -1;
public MyList()
{
array = new T[3];
}
public T this[int index]
{
get
{
return array[index];
}
set
{
if (index >= array.Length)
{
Array.Resize<T>(ref array, index + 1);
Console.WriteLine("Array Resized : {0}", array.Length);
}
array[index] = value;
}
}
public int Length
{
get { return array.Length; }
}
public IEnumerator<T> GetEnumerator()
{
for (int i = 0; i < array.Length; i++)
{
yield return (array[i]);
}
}
IEnumerator IEnumerable.GetEnumerator()
{
for (int i =0; i < array.Length; i++)
{
yield return (array[i]);
}
}
public T Current
{
get { return array[position]; }
}
object IEnumerator.Current
{
get { return array[position]; }
}
public bool MoveNext()
{
if (position == array.Length - 1)
{
Reset();
return false;
}
position++;
return (position < array.Length);
}
public void Reset()
{
position = -1; ;
}
public void Dispose()
{
}
}s
class MainApp
{
static void Main(string[] args)
{
MyList<string> str_list = new MyList<string>();
str_list[0] = "abc";
str_list[1] = "edf";
str_list[2] = "ghi";
str_list[3] = "jki";
str_list[4] = "mno";
foreach (string str in str_list)
Console.WriteLine(str);
Console.ReadLine();
MyList<int> int_list = new MyList<int>();
int_list[0] = 0;
int_list[1] = 1;
int_list[2] = 2;
int_list[3] = 3;
int_list[4] = 4;
foreach (int no in int_list)
Console.WriteLine(no);
}
}
}
|
cs |
