class MyList<T> : IEnumerable<T>, IEnumerator<T> {
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 : {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() {
}
}
class MainApp {
static void Main(string[] args) {
MyList<int> int_list = new MyList<int>();
int_list[0] = 10;
int_list[1] = 20;
int_list[2] = 30;
int_list[3] = 40;
int_list[4] = 50;
foreach (var item in int_list) {
Console.WriteLine(item);
}
}
}
정상 출력